user1511924
user1511924

Reputation: 53

know which keyword of regex was found

If I have the following code:

Pattern p = Pattern.compile("Fiat|Panda|Ford");

String searchStr =  "Fiat Panda 4747 ";
Matcher m = p1.matcher(searchStr);
while(m.find()) {
    System.out.println(m.group());
}

Is it possible to know which of the keywords "Fiat", "Panda" or "Ford" was found?

Upvotes: 1

Views: 61

Answers (1)

hwnd
hwnd

Reputation: 70732

This should do it. You had p1.matcher, instead you need to change it to p.matcher.

String in = "Fiat Panda 4747";
Pattern p = Pattern.compile("Fiat|Panda|Ford");
Matcher m = p.matcher(in);
while (m.find()) {
  System.out.println(m.group());
}

Upvotes: 1

Related Questions