Steve
Steve

Reputation: 229

How to create an OR condition in regex?

I was wondering, how do I create create a regular expression that allows me to use something akin an "or" statement. For example,

^\w+? [\s?] OR [\\w\\W]*?]$

where the brackets represent the condition.

Upvotes: 0

Views: 60

Answers (2)

Josh M.
Josh M.

Reputation: 27791

Like this:

(thing1|thing2)

This will match either "thing1" or "thing2" - see here for more info.

In your example it would be:

^(\w+? [\s?]|[\w\W]*?])$

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 190943

Use a | between your groups.

Upvotes: 2

Related Questions