Nikolai Sleta Nikolai Sleta - 1 month ago 13
PHP Question

check words sequence regexp

I have some text and have to check is it valid or no.


fddfgdfgfdg phrase here first

sdafdsafdsa

sadfsadf

sadfdsaf

phrase here second


I have to check in this text the correct sequence.
Word FIRST must be in text before word SECOND. We know only these two words and don't know the rest text. So "first ANY TEXT second" - valid, "second ANY TEXT first" - not valid.

I wrote a regexp
/^.*?\n*?first.*?|\n*?second.*?\n*?$/mg
, but in finds words in any sequence.

Answer

The /^.*?\n*?first.*?|\n*?second.*?\n*?$/mg regex will match first and second separately, without considering the order since the pattern consists of 2 alternatives, 1) ^.*?\n*?first.*? that matches start of a line, any 0+ chars up to 0+ newlines and then first and 2) \n*?second.*?\n*?$ that matches 0+ newlines, second then any 0+ chars up to optional newlines and end of line. Actually, the pattern is certainly a bit off.

You may use the following regex:

'~first.*second~s'

See the regex demo

Use with preg_match. The pattern will find first and then second somewhere after first across multiple lines due to the s modifier.

An efficient alternative unrolled regex is '~first[^s]*(?:s(?!econd)[^s]*)*second~' that will find first and then second after it.

See PHP demo

$re = '~first.*second~s';
$str = "fddfgdfgfdg  phrase here first\nsdafdsafdsa\nsadfsadf\nsadfdsaf\nphrase here second";
if (preg_match($re, $str, $matches)) {
    echo "Valid!";
}

If the first and second are whole alphanumeric words, enclose with word boundaries: \bfirst\b and \bsecond\b.