You can place all the places in one group and get this group value:
.*(Venue1|Venue2|Venue3).*
In the regular expression above, the associated venue will consist of a group . (I assume that your places are just examples if they cannot simplify things 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 .
source
share