Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex: matches(pattern, value) returns true but group() fails to match

Tags:

java

regex

I have an odd problem with a regular expression in Java. I tested my Regex and my value here and it works. It says there are 3 groups (correct) the match for the first group (not group zero!) is SSS, the match for group 2 is BB and the match for group 3 is 0000. But my code below fails and I am quite at a loss why...

String pattern = "([^-]*)-([\\D]*)([\\d]*)";
String value = "SSS-BB0000";
Matcher matcher = Pattern.compile(pattern).matcher(value);
//group() is equivalent to group(0) - it fails to match though
matcher.group();

Here is a screenshot from the matching result of the above website: enter image description here

I'd be really grateful if anyone could point out the mistake I am making... On an additional note: Strangely enough, if I execute the following code true is returned which implies a match should be possible...

//returns true
Pattern.matches(pattern, value);
like image 442
Ready4Android Avatar asked Aug 11 '11 21:08

Ready4Android


People also ask

What is the difference between match and group regex?

A Match is an object that indicates a particular regular expression matched (a portion of) the target text. A Group indicates a portion of a match, if the original regular expression contained group markers (basically a pattern in parentheses).

What is difference between matches () and find () in Java regex?

Difference between matches() and find() in Java RegexThe matches() method returns true If the regular expression matches the whole text. If not, the matches() method returns false. Whereas find() search for the occurrence of the regular expression passes to Pattern.

Does * match everything in regex?

Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.

What does \\ mean in Java regex?

Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.


1 Answers

You need to call find() before group():

String pattern = "([^-]*)-([\\D]*)([\\d]*)"; 
String value = "SSS-BB0000";
Matcher matcher = Pattern.compile(pattern).matcher(value); 
if (matcher.find()) {
  System.out.println(matcher.group()); // SSS-BB0000
  System.out.println(matcher.group(0)); // SSS-BB0000
  System.out.println(matcher.group(1)); // SSS
  System.out.println(matcher.group(2)); // BB
  System.out.println(matcher.group(3)); // 0000
}

When you invoke matcher(value), you are merely creating a Matcher object that will be able to match your value. In order to actually scan the input, you need to use find() or lookingAt():

References:

  • Matcher#find()
like image 190
João Silva Avatar answered Oct 02 '22 08:10

João Silva