Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find two words on the page

I'm trying to find all pages which contain words "text1" and "text2". My regex:

text1(.|\n)*text2  

it doesn't work.. enter image description here

like image 568
DmitMedv Avatar asked Nov 12 '14 15:11

DmitMedv


People also ask

How do you find multiple words in a regular expression?

However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).


1 Answers

If your IDE supports the s (single-line) flag (so the . character can match newlines), you can search for your items with:

(text1).*(text2)|\2.*\1 

Example with s flag

If the IDE does not support the s flag, you will need to use [\s\S] in place of .:

(text1)[\s\S]*(text2)|\2[\s\S]*\1 

Example with [\s\S]

Some languages use $1 and $2 in place of \1 and \2, so you may need to change that.

EDIT:

Alternately, if you want to simply match that a file contains both strings (but not actually select anything), you can utilize look-aheads:

(?s)^(?=.*?text1)(?=.*?text2) 

This doesn't care about the order (or number) of the arguments, and for each additional text that you want to search for, you simply append another (?=.*?text_here). This approach is nice, since you can even include regex instead of just plain strings.

like image 76
OnlineCop Avatar answered Oct 02 '22 18:10

OnlineCop