regex: How to extract substrings?

M

Markus Dehmann

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!
 
K

Knute Johnson

Markus said:
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));
}
 
I

IchBin

Markus said:
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-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top