Reputation: 16651
I have a string like follows:
@78517700-1f01-11e3-a6b7-3c970e02b4ec
, @68517700-1f01-11e3-a6b7-3c970e02b4ec
, @98517700-1f01-11e3-a6b7-3c970e02b4ec
, @38517700-1f01-11e3-a6b7-3c970e02b4ec
....
I want to extract the string after @
.
I have the current code like follows:
private final static Pattern PATTERN_LOGIN = Pattern.compile("@[^\\s]+");
Matcher m = PATTERN_LOGIN.matcher("@78517700-1f01-11e3-a6b7-3c970e02b4ec , @68517700-1f01-11e3-a6b7-3c970e02b4ec, @98517700-1f01-11e3-a6b7-3c970e02b4ec, @38517700-1f01-11e3-a6b7-3c970e02b4ec");
while (m.find()) {
String mentionedLogin = m.group();
.......
}
... but m.group()
gives me @78517700-1f01-11e3-a6b7-3c970e02b4ec
but I wanted 78517700-1f01-11e3-a6b7-3c970e02b4ec
Upvotes: 2
Views: 128
Reputation: 2903
Correct answers are mentioned in other responses. I will add some clarification. Your code is working correctly, as expected.
Your regex means: match string which starts with @
and after that follows one or more characters which isn't white space. So if you omit the parentheses you get you full string as expected.
The parentheses as mentioned in other responses are used for marking capturing groups. In layman terms - the regex engine does the matching multiple times for each parenthesis enclosed group, working it's way inside the nested structure.
Upvotes: 0
Reputation: 1948
You should use the regex "@([^\\s]+)"
and then m.group(1)
, which returns you what "captured" by the capturing parentheses ()
.
m.group()
or m.group(0)
return you the full matching string found by your regex.
Upvotes: 3
Reputation: 47945
I would modify the pattern to omit the at sign:
private final static Pattern PATTERN_LOGIN = Pattern.compile("@([^\\s]+)");
So the first group will be the GUID only
Upvotes: 3