我需要一段PHP代码来检测一个字符串中是否包含4个或更多连续的数字(0到9),例如:
"one four six nine five"
或者
"zero eight nine nine seven three six six"
\b(?:(?:one|two|three|four|five|six|seven|eight|nine|zero)\b\s*?){4}
这与其余部分基本相同。唯一有趣的部分是\s*?
- 它将懒惰地匹配单词之间的空格,因此您不会在4个单词序列之后得到额外的空格。\b
位于其前面,确保至少有一个空格(或其他分隔符在最后一个单词之后,因此!a b c d!
将匹配)。
:)
。 - Kobi/(?:(?:^|\s)(?:one|two|three|four|five|six|seven|eight|nine|ten)(?=\s|$)){4,}/
PHP 代码:
if (preg_match(...put regex here..., $stringToTestAgainst)) {
// ...
}
"/.../"
),并且您可能需要转义\s
(=>\\s
)。 - Jamesif (preg_match("/(?:\b(?:(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))\b\s*?){4,}/", $variable_to_test_against, $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
编辑:
在正则表达式中添加了空格 - 感谢Kobi。