Say I have a regular expression like the fowllowing: {"(group 1) | (group 2) (group 3) |....( group n)"} to match an input String object, if it matches successfully, how can I know which group among above n groups is acturally matches this String object? What I am using is regex lib in java.util. Thank you guys.
views:
71answers:
2
+3
A:
Here's a way:
import java.util.regex.*;
public class Test {
public static void main(String[] args) {
String text = "12 ab ^&";
String regex = "(\\d+)|([a-z]+)|(\\p{Punct}+)";
Matcher m = Pattern.compile(regex).matcher(text);
while(m.find()) {
System.out.println("\nmatched text: "+m.group());
for(int i = 1; i <= m.groupCount(); i++) {
System.out.println(" group "+i+"? "+(m.group(i) != null));
}
}
}
}
output:
matched text: 12
group 1? true
group 2? false
group 3? false
matched text: ab
group 1? false
group 2? true
group 3? false
matched text: ^&
group 1? false
group 2? false
group 3? true
Bart Kiers
2010-02-04 13:58:49
Thank you guys, but what I am persuing is getting this group by just one method call instead of doing all the for loop checking. Because I have a whole large number of String objects need to be matched, and don't know if this checking later in a for loop will impair the performance..
Jian.Hu
2010-02-04 14:15:20
If you're worried about performance, don't use regex at all. Besides, without looping, it's not possible to get the group number: believe me. What is a "large number of Strings", btw? And how large (how many characters) do these Strings have?
Bart Kiers
2010-02-04 14:17:38
hmmm, all the matching job will be done in a thread, and each time runs the run() method it will have like thousands Strings to match...the regular expression might have 10-20 groups. So consider 1000 loops, this performance consideration rises itself..Strings need to handle are not big one, usually less then 100 characters.Any way worth a try. thanks.
Jian.Hu
2010-02-04 14:50:19
You're welcome Jian.
Bart Kiers
2010-02-04 15:16:19
By the way, concatenating a bunch of strings into one big OR regex is almost always slower than searching for each individually. Regex will start at position 0, check every OR branch, move to position 1, check every OR branch, and so on. Depending on what you are actually trying to find (first match, all matches, etc.) there are a half a dozen better/faster ways to search.
PSpeed
2010-02-04 22:33:52
+2
A:
You can use the methods on the Matcher object to see which groups matched. Here's an example:
import java.util.regex.*;
class RegexExample
{
public static void main(String[] args) {
String input = "foo baz bar foo";
String regex = "(foo)|(bar)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); ++i) {
if (matcher.group(i) != null)
{
System.out.println("Group " + i + " matched.");
}
}
}
}
}
Output:
Group 1 matched.
Group 2 matched.
Group 1 matched.
Mark Byers
2010-02-04 14:00:34