regex: How to extract substrings?  
Author Message
Markus Dehmann





PostPosted: 2005-12-10 13:12:00 Top

java-programmer, regex: How to extract substrings? 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





PostPosted: 2005-12-10 14:31:00 Top

java-programmer >> regex: How to extract substrings? Markus Dehmann wrote:
> 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!

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));
}

--

Knute Johnson
email s/nospam/knute/
 
IchBin





PostPosted: 2005-12-10 14:36:00 Top

java-programmer >> regex: How to extract substrings? Markus Dehmann wrote:
> 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!


Actually, Roedy has a nice reference for this

http://mindprod.com/jgloss/regex.html
--


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-)