Anton Belev
Anton Belev

Reputation: 13533

Find which part of a regular expression caused a match

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?

Upvotes: 4

Views: 92

Answers (3)

acdcjunior
acdcjunior

Reputation: 135762

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.

Upvotes: 4

Laur Ivan
Laur Ivan

Reputation: 4177

You could use groups. Something along the lines of this may solve tour problem. Vogella also has some examples. Googling "java regex group" will give you quite a lot of references :)

Upvotes: 0

Guntram Blohm
Guntram Blohm

Reputation: 9819

Use (.*)Venue([123])(.*), then check what's between the 2nd pair of brackets.

Upvotes: 2

Related Questions