merveotesi
merveotesi

Reputation: 2193

How can I provide an OR operator in regular expressions?

I want to match my string to one sequence or another, and it has to match at least one of them.

For and I learned it can be done with:

(?=one)(?=other)

Is there something like this for OR?

I am using Java, Matcher and Pattern classes.

Upvotes: 6

Views: 28521

Answers (6)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

While the | alternation operator is the answer to "what is the alternation operator in regex", there are many more nuances in using it inside regular expressions.

I highly suggest enclosing these patterns within a non-capturing group:

(?:one|other)

because more ofthen than not, these alternations appear as part of longer regex patterns, and in most cases, you really do not care about which word was the actual context in the text.

Capturing groups ((one|other)) allocate memory for the captured parts of the match, which involves some overhead. Non-capturing groups ((?:one|other)) allow you to group multiple patterns together without storing the matched text for later reference.

You want to match the strings as whole words? Use \b(?:one|other)\b, not \bone|other\b (see Regular expression pipe confusion why). Trying to extract an ID number after several words into a single Pandas column using Series.str.extract? Use (?:one|other)\s*(\d+).

The non-capturing group syntax (?:...) is widely supported in modern regex flavors, it is easier to list environments where it is not supported: in POSIX Basic Regular Expressions (BRE) in old Unix systems and older versions of sed and grep without extended regex, in XSD (XML Schema) regex, and in Lua patterns (that are not regular expressions in the first place, but some consider them that).

Upvotes: 0

ppeterka
ppeterka

Reputation: 20736

Generally speaking about regexes, you definitely should begin your journey into Regex wonderland here: Regex tutorial

What you currently need is the | (pipe character)

To match the strings one OR other, use:

(one|other)

or if you don't want to store the matches, just simply

one|other

To be Java specific, this article is very good at explaining the subject

You will have to use your patterns this way:

//Pattern and Matcher
Pattern compiledPattern = Pattern.compile(myPatternString);
Matcher matcher = pattern.matcher(myStringToMatch);
boolean isNextMatch = matcher.find(); //find next match, it exists, 
if(isNextMatch) {
    String matchedString = myStrin.substring(matcher.start(),matcher.end());
}

Please note, there are much more possibilities regarding Matcher then what I displayed here...

//String functions
boolean didItMatch = myString.matches(myPatternString); //same as Pattern.matches();
String allReplacedString = myString.replaceAll(myPatternString, replacement)
String firstReplacedString = myString.replaceFirst(myPatternString, replacement)
String[] splitParts = myString.split(myPatternString, howManyPartsAtMost);

Also, I'd highly recommend using online regex checkers such as Regexplanet (Java) or regex101, they make your life a lot easier!

Upvotes: 12

Robe Elckers
Robe Elckers

Reputation: 977

The answers are already given, use the pipe '|' operator. In addition to that, it might be useful to test your regexp in a regexp tester without having to run your application, for example:

http://www.regexplanet.com/advanced/java/index.html

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Use the | character for OR

Pattern pat = Pattern.compile("exp1|exp2");
Matcher mat = pat.matcher("Input_data");

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272427

You can separate with a pipe thus:

Pattern.compile("regexp1|regexp2");

See here for a couple of simple examples.

Upvotes: 2

NPE
NPE

Reputation: 500913

The "or" operator is spelled |, for example one|other.

All the operators are listed in the documentation.

Upvotes: 4

Related Questions