Logical operator AND with php regular expression

13,884

Solution 1

PHP does support lookahead expressions. You're probably not using them correctly, though.

Assuming you want to match a string that contains all three of foo, bar and baz, you need the regex

^(?=.*foo)(?=.*bar)(?=.*baz)

This will return a match for the strings foobarbaz or barbazfoo etc. However, that match will be the empty string (because the lookaheads don't consume any characters). If you want the regex to return the string itself, use

^(?=.*foo)(?=.*bar)(?=.*baz).*

which will then match the entire string if it fulfills all three criteria.

I would simply use

if (preg_match('/^(?=.*foo)(?=.*bar)(?=.*baz)/s', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}

Take note that this will also match a string like foonly bartender bazooka. If you don't want that (only allowing pure permutations of one each of the three expressions), you can do it with a little trick:

^(?:foo()|bar()|baz()){3}\1\2\3$

matches foobarbaz, foobazbar, barfoobaz, barbazfoo, bazfoobar and bazbarfoo (and nothing else). The "trick" is inspired by Jan Goyvaerts' and Steven Levithan's excellent book "Regular Expressions Cookbook" (p. 304). It works as follows:

  • Each required part (foo etc.) is followed by an empty capturing group () which always matches if the required part has been matched.
  • So if all three required parts have matched, all three empty capturing groups have matched.
  • The following backreferences only succeed if each of the capturing groups has participated in the match.
  • So if the string is foobarbar, the part (?:foo()|bar()|baz()){3} will have matched, but \3 fails, so the overall regex fails.
  • If, however, all three did take part in the match, \1\2\3 succeeds in matching at the end of the string because each of the capturing groups contains nothing but the empty string.

Solution 2

In addition to @Tim's answer:

(?=exp1)(?=exp2)

This can never be true. You say in plain english : make sure that the text followed here is exp1 and also make sure that the text followed here is exp2. No way this is true. It will never match.

Share:
13,884
Belsen
Author by

Belsen

Updated on June 04, 2022

Comments

  • Belsen
    Belsen almost 2 years

    I'd like to use a kind of logical operator "AND" in my regular expression. I tried this:

    (?=exp1)(?=exp2)
    

    But in PHP ?= doesn't work and need to write my program in PHP language. Is there another method? The expression has to match if there are present all the conditions and in any order. I don't wanna write every permutation like:

    (exp1)(exp2)(exp3)|(exp1)(exp3)(exp2)|....
    
  • bozdoz
    bozdoz over 12 years
    @tim could you provide resources or supplementary on this? I've never seen something like foo()|bar()|baz() before.
  • bozdoz
    bozdoz over 12 years
    I guess, what I'm asking is what is the significance of the empty brackets?
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    @bozdoz: I added an explanation and reference.