• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex - Pattern Matching Java - Error matching complete sentence.  RSS feed

 
Dhivya Krishnan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am finding issues in matching the below pattern.

The pattern works when the string is st2 and when st1 is given it goes into an infinite loop as a part of it is matching.

Can someone please let me know how to handle it.?

public class PatternMatching {

public static void main (String args[]) throws Exception {

// problem with matching whole string
String st1 = "850 - PATTERN BUNDLES 10/2014 805 - 922 - 5225 245.00";
String st2 = "850 - PATTERN BUNDLES 10/12/2014 805 - 922 - 5225 245.00";
Pattern pattern1 = Pattern.compile("(\\d+)\\s*\\-?\\s*(([\\,a-zA-Z.-]*\\s*)+)\\s*((\\d{1,2}\\/\\d{1,2}\\/\\d{4})|(\\d{1,2}\\-\\d{1,2}))\\s+(\\d{3}\\s*\\-?\\s*\\d{3}\\s*\\-?\\s*\\d{4})\\s*(\\d+\\,?\\d*\\.\\d+)");
Matcher matcher1 = pattern1.matcher(st2);
if (matcher1.find()) {
System.out.println("MAtch Group -->"+matcher1.group());
}
}
}
 
g tsuji
Ranch Hand
Posts: 697
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has not much to do with ws and will probably be moved to an more appropriate forum sooner or later.

In the meantime, your pattern certain won't match with st1. (I don't understand the infinite loop statement, but that's not important.) This should make it work.
 
Dhivya Krishnan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Now I am facing one more problem.

String st1 = "849 Project yellow, BY September 2014 9/2014 215 - 687 - 3944 245.00";
Pattern pattern = Pattern.compile("(\\d+)\\s*\\-?\\s*(([\\,a-zA-Z.-]*\\s*)+)\\s*((\\d{1,2}\\/\\d{1,2}\\/\\d{4})|(\\d{1,2}\\-\\d{1,2}))\\s+(\\d{3}\\s*\\—?\\s*\\d{3}\\s*\\-?\\s*\\d{4})\\s*(\\d+\\,?\\d*\\.\\d+)");
Pattern pattern = Pattern.compile("(\\d+)\\s*\\-?\\s*(([\\,a-zA-Z.-0-9]*\\s*)+)\\s*");

Matcher matcher = pattern.matcher(st1);
if (matcher.find()) {
System.out.println("MAtch Group -->"+matcher.group());
}

Here i want to seperate Project yellow, BY September 2014 9/2014 into Project yellow, BY September 2014 and 9/2014 .

Can someone help me write pattern for this.
If i give *(([\\,a-zA-Z.-0-9]*\\s*)+) it will give me Project yellow, BY September 2014 9. I dont want the date field to be mixed here.
 
Dhivya Krishnan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this problem often, so want your help to resolve.

My regex pattern should match the complete sentence, if not it should not get into the loop.

String st = "CAP PAL 0001 ADV LS C 74707 2430 3140031230 12 5492 956 --Outrage-24 Hou 364";
String st1 = "CAP APL PL ASDV BUSA CI 14477 1040 3102684626 551 5492 956 --In-Tak Center 4 N Replic 969-61670";
String st2 = " DIRECTORY EXTRACT VIEW ";

Pattern pa1 = Pattern.compile("^(([a-zA-Z-]*\\s*)+)\\s*\\d{5}\\s*\\d{4}\\s*\\d{10}\\s*\\d{2,3}\\s*\\d{4}\\s*\\d{3}\\s*(([-a-zA-Z0-9]*\\s*)+)\\s*$");
Matcher mat1 = pa1.matcher(st);

if (mat1.find()) {
System.out.println("Match Main Group -->"+mat1.group());
}

Here it works for st1, for both st and st2 it goes into infinite loop.

Can someone explain me why does that happen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!