This should be really easy, but I couldn't find it in the tutorials and
documentations on the web:
What is the Java equivalent to the following Perl operation?
my $entry = "__3432__Smith__"
my ($id,$name) =
$entry =~ m/__(\d+)__([A-z]+)__/;
In other words, I want to extract the number and the name from the
string $entry, using a regular expression.
I tried the following:
Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__");
Matcher m = p.matcher("__3432__Smith__");
while(m.find()){
System.out.println(m.group());
}
But that gives me the complete match at once, not the two subgroups that
I specified using the parens: (\d+) and ([A-z]+).
Who can help?
Thanks!
Knute Johnson - 10 Dec 2005 06:31 GMT
> This should be really easy, but I couldn't find it in the tutorials and
> documentations on the web:
[quoted text clipped - 21 lines]
> Who can help?
> Thanks!
From the docs on Matcher
"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()."
So to answer your question:
Pattern p = Pattern.compile("__(\\d+)__(\\w+)__");
Matcher m = p.matcher($entry);
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}

Signature
Knute Johnson
email s/nospam/knute/
IchBin - 10 Dec 2005 06:35 GMT
> This should be really easy, but I couldn't find it in the tutorials and
> documentations on the web:
[quoted text clipped - 21 lines]
> Who can help?
> Thanks!
Actually, Roedy has a nice reference for this
http://mindprod.com/jgloss/regex.html

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)