Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp logic and or

I know there are logical operators such as | "the OR operator" which can be used like this:

earth|world 

I was wondering how I could check if my string contains earth AND world.

regards, alexander

like image 886
Alexander Avatar asked Mar 03 '11 03:03

Alexander


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

Can you chain regex?

Chaining regular expressionsRegular expressions can be chained together using the pipe character (|). This allows for multiple search options to be acceptable in a single regex string.

What is regex logic?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

If it contains earth AND world, it contains one after the other, so:

earth.*world|world.*earth 

an shorter alternative (using extended regex syntax) would be:

/(?=.*earth)(?=.*world).*/ 

But it is not at all like an and operator. You can only do or because if only one of the words is included, there is no ordering involved. If you want to have them both, you need to indicate the order.

like image 174
markijbema Avatar answered Sep 22 '22 11:09

markijbema