Retrieving the matches to a regular expression

S

Siam

Hi all,

Is there any way of not only checking a regular expression matches a
string, but also grabbing the the values that the wildcards matched to?

e.g, we know "Coordinate 234 124 12" matches with "Coordinate \\d++
\\d++ \\d++", but how could we actually get the values 234, 124 and 12
that matched the numbers in the pattern? I would've thought this would
by quite commonly done, but all I could find in the APIs were simple
boolean tests. I want to avoid simply scanning my way through the
string manually, cos I'm sure this was already done when doing the
matching.

Any ideas?

Cheers,

Siam
 
E

Eric Sosman

Siam said:
Hi all,

Is there any way of not only checking a regular expression matches a
string, but also grabbing the the values that the wildcards matched to?

e.g, we know "Coordinate 234 124 12" matches with "Coordinate \\d++
\\d++ \\d++", but how could we actually get the values 234, 124 and 12
that matched the numbers in the pattern? I would've thought this would
by quite commonly done, but all I could find in the APIs were simple
boolean tests. I want to avoid simply scanning my way through the
string manually, cos I'm sure this was already done when doing the
matching.

Go back to the documentation for java.util.regex.Pattern
and look for the section entitled "Groups and Capturing."
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top