Find which part of a regular expression caused a match
java, regex
Solution
You can place all venues within a single group and get that group's value:
.*(Venue1|Venue2|Venue3).*
In the regex above, the matched venue would be group one. (I'm assuming your venues are just examples, if they aren't you could simplify further `.*(Venue[123]).*`.)
After that, you can use `Matcher#group(int)`:
public static void main(String[] args) throws java.lang.Exception {
checkVenue("Test Venue1 test test");
checkVenue("Test Venue2 test test");
checkVenue("Test Venue3 test test");
checkVenue("Test Venue1 Venue3 test");
}
public static void checkVenue(String tweet) {
Pattern p = Pattern.compile(".*(Venue1|Venue2|Venue3).*");
Matcher m = p.matcher(tweet);
System.out.print(tweet + ":\t ");
if (m.find()) {
System.out.println("found " + m.group(1));
} else {
System.out.println("found none.");
}
}
Output:
Test Venue1 test test: found Venue1
Test Venue2 test test: found Venue2
Test Venue3 test test: found Venue3
Test Venue1 Venue3 test: found Venue3
Run this demo online here.
Problem
I've got a regular expression of the following format: ``` ((.*)Venue1(.*)) | ((.*)Venue2(.*)) | ((.*)Venue3(.*)) ``` Then I've got some twitter messages and using this regular expression I'm finding if a venue is mentioned in the messages (I know that this method has some bugs, but at the moment is fine for me). However in that way I don't know which exactly venue was mentioned, because I'm using `tweet.matches(regex)`. I was thinking to break the whole regex and check the twitter message against each venue name separately. Do you think there is a faster way to check, which venue namer from the long regex, caused the match?