FirmView
FirmView

Reputation: 3150

Regular expression matching whole word OR operator

I am trying to match full word from some lines, wanted to know how to use the OR in regex, If i use only one keyword, it works fine. Example,

regex = ".*\\b" + "KEYWORD1" + "\\b.*";


String regex = ".*\\b" + "KEYWORD1|KEYWORD2|KEYWORD3" + "\\b.*";

    for (int i = start; i < end; i++) {           
        if (lines[i].matches(regex)) {
            System.out.println("Matches");
        }
    }

Upvotes: 3

Views: 13882

Answers (3)

LucDaher
LucDaher

Reputation: 157

I know this issue has been solved a long time ago but I'd like to contribute adding another possibility for a different scenario.

For those (like me) who's facing the challenge of interpreting an entire phrase (not just a word) inside regex's OR statement, surrounding it with "start" and "end" ^(...|...)$ do the trick.

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208705

The pipe character | can be used as an OR operator, which is called alternation in regex.

To get this to work properly in your example, you just need to create a group around the alternation to be sure that you are doing the OR only on the keywords you are interested in, for example:

String regex = ".*\\b(KEYWORD1|KEYWORD2|KEYWORD3)\\b.*";

What you currently have would mean .*\\bKEYWORD1 OR KEYWORD2 OR KEYWORD3\\b.*.

Upvotes: 3

Kendall Frey
Kendall Frey

Reputation: 44374

You want:

String regex = ".*\\b(KEYWORD1|KEYWORD2|KEYWORD3)\\b.*";

Originally, your regex was being evaluated like this:

.*\bKEYWORD1
|
KEYWORD2
|
KEYWORD3\b.*

But you want:

.*\b
(
    KEYWORD1
    |
    KEYWORD2
    |
    KEYWORD3
)
\b.*

This cool tool can help you analyse regexes and find bugs like this one.

Upvotes: 11

Related Questions