Find which part of the regular expression caused a match

I have a regular expression of the following format:

((.*)Venue1(.*)) | ((.*)Venue2(.*)) | ((.*)Venue3(.*))

Then I have some posts on Twitter and use this regex that I find if the posts mention the meeting place (I know this method has some errors, but for now this is good for me). However, this way I don’t know which place was mentioned because I use tweet.matches(regex). I thought to break all the regular expression and check the Twitter message for each place name separately. Do you think there is a faster way to check which meeting place with a long regular expression caused a match?

+4
source share
3 answers

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 .

+4
source

Use (.*)Venue([123])(.*), then check that between the second pair of brackets.

+2
source

. - . Vogella . Googling "java regex group" :)

0

All Articles