逻辑运算符并带有php正则表达式 [英] Logical operator AND with php regular expression

查看:112
本文介绍了逻辑运算符并带有php正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的正则表达式中使用一种逻辑运算符"AND". 我试过了:

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

(?=exp1)(?=exp2)

但是在PHP中,?=不起作用,需要用PHP语言编写程序.还有其他方法吗?如果存在所有条件且以任何顺序存在,则表达式必须匹配. 我不想像这样写每个排列:

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)|....

推荐答案

PHP确实支持超前表达式.不过,您可能没有正确使用它们.

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

假设您要匹配包含foobarbaz的全部三个的字符串,则需要使用正则表达式

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

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

这将返回字符串foobarbazbarbazfoo等的匹配项.但是,该匹配项将为空字符串(因为先行符不消耗任何字符).如果要让正则表达式返回字符串本身,请使用

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.

我会简单地使用

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

请注意,这也将匹配foonly bartender bazooka之类的字符串.如果您不希望这样做(仅允许三个表达式之一进行纯置换),则可以使用一些技巧:

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$

匹配foobarbazfoobazbarbarfoobazbarbazfoobazfoobarbazbarfoo(没有其他内容). 技巧"的灵感来自Jan Goyvaerts和Steven Levithan的出色著作正则表达式食谱" (第304页).它的工作方式如下:

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:

  • 每个必需部分(foo等)后跟一个空的捕获组(),如果匹配了必需部分,则该捕获组将始终匹配.
  • 因此,如果所有三个必需部分均已匹配,则所有三个空捕获组均已匹配.
  • 仅当每个捕获组都参加了比赛时,以下反向引用才会成功.
  • 因此,如果字符串为foobarbar,则部分(?:foo()|bar()|baz()){3}将匹配,但是\3失败,因此整个正则表达式将失败.
  • 但是,如果所有三个组都参加了比赛,则\1\2\3会在字符串末尾成功进行匹配,因为每个捕获组只包含空字符串.
  • 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.

这篇关于逻辑运算符并带有php正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆