System.out.println(matcher.group(1));
System.out.println(matcher.group());
I like to know what’s the difference between the above two codes. I get different outputs. Can anybody elaborate on this?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The call to
group()gives you the entire string that matched, whereasgroup(1)gives you the first parenthesized “Capturing” group (or more generally,group(n)will give you the n’th capturing group, counting left/opening parenthesis, starting from 1).So for example, if you had an input string like this:
And you matched against the following regular expression (without the quotes):
Then
group()would give you “The quick” andgroup(1)would give you “quick”.For more details on how all of this regular expression stuff works in Java, look See the
java.util.regex.MatcherJavaDoc.