1

リストされていない単語が検出されない限り、任意の順序で単語のリストに一致する正規表現を探しています。コードは次のようなものになります

// match one two and three in any order
$pattern = '/^(?=.*\bone\b)(?=.*\btwo\b)(?=.*\bthree\b).+/';
$string = 'one three';
preg_match($pattern, $string, $matches);
print_r($matches); // should match array(0 => 'one', 1 => 'three')

// match one two and three in any order
$pattern = '/^(?=.*\bone\b)(?=.*\btwo\b)(?=.*\bthree\b).+/';
$string = 'one three five';
preg_match($pattern, $string, $matches);
print_r($matches); // should not match; array() 
4

4 に答える 4

2

先読みを必要とせずにこれを行うことができるはずです。

のようなパターンを試してください

^(one|two|three|\s)+?$

上記は、、、、または空白文字に一致oneします。twothree\s

于 2013-09-11T16:20:18.880 に答える
2

これを試してください:

$pattern = '/^(?:\s*\b(?:one|two|three)\b)+$/';
于 2013-09-11T16:32:48.137 に答える
0

「1、2、3」のすべてが必要で
、別の単語がない場合、これは機能します。

 # ^(?!.*\b[a-zA-Z]+\b(?<!\bone)(?<!\btwo)(?<!\bthree))(?=.*\bone\b)(?=.*\btwo\b)(?=.*\bthree\b)

 ^ 
 (?!
      .* \b [a-zA-Z]+ \b 
      (?<! \b one )
      (?<! \b two )
      (?<! \b three )
 )
 (?= .* \b one \b )
 (?= .* \b two \b )
 (?= .* \b three \b )
于 2013-09-11T18:11:51.010 に答える