Reputation: 96797
I'm trying to match the username with a regex. Please don't suggest a split.
USERNAME=geo
Here's my code:
String input = "USERNAME=geo";
Pattern pat = Pattern.compile("USERNAME=(\\w+)");
Matcher mat = pat.matcher(input);
if(mat.find()) {
System.out.println(mat.group());
}
why doesn't it find geo
in the group? I noticed that if I use the .group(1)
, it finds the username. However the group
method contains USERNAME=geo
. Why?
Upvotes: 2
Views: 421
Reputation: 383746
Because group()
is equivalent to group(0)
, and group 0 denotes the entire pattern.
From the documentation:
public String group(int group)
Group zero denotes the entire pattern, so the expression
m.group(0)
is equivalent tom.group()
As you've found out, with your pattern, group(1)
gives you what you want.
If you insist on using group()
, you'd have to modify the pattern to something like "(?<=USERNAME=)\\w+"
.
Upvotes: 5
Reputation: 881383
That's because group
is supposed to return the string matching the pattern in its entirety. For getting a group within that string, you need to pass the group number that you want.
See here for details, paraphrased below:
group
public String group()
Returns the input subsequence matched by the previous match.public String group(int group)
Returns the input subsequence captured by the given group during the previous match operation.
Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().
Upvotes: 1
Reputation: 89169
For your solution, here's what works:
public static void main(String[] args) {
String input = "USERNAME=geo";
Pattern pat = Pattern.compile("USERNAME=(\\w+)");
Matcher mat = pat.matcher(input);
if(mat.find()) {
System.out.println(mat.group(1));
}
}
Output geo
Reason
String java.util.regex.Matcher.group(int group)
Returns the input subsequence captured by the given group during the previous match operation.For a matcher m, input sequence s, and group index g, the expressions m.group(g) and s.substring(m.start(g), m.end(g)) are equivalent.
Upvotes: 1
Reputation: 32484
So the VAR.group( int i ) will return the ith capture group of the regex. With 0 being the full string. You need to call .group( 1 )
Upvotes: 1
Reputation: 1828
As Matcher.group()
javadoc says, it "returns the input subsequence matched by the previous match", and the previous match in your case was "USERNAME=geo" since you've called find()
.
In contrast, the method group(int)
returns specific group. Capturing groups are numbered by counting their opening parentheses from left to right, so the first group would match "geo" in your case.
Upvotes: 3