Php 只有在字符串中没有其他数字时,正则表达式才会捕获两位数字

Php 只有在字符串中没有其他数字时,正则表达式才会捕获两位数字,php,regex,Php,Regex,我只想在字符串中没有其他数字时查找并捕获两位数字,我的输入: foo 12 ✅ foo 12 bar ✅ 12 baz ✅ 9 foo 12 ❌ foo 12 baz 2 ❌ 12 2 ❌ 我试过这个模式 preg\u match(“`(?您需要检查整个字符串是否包含任何其他数字,因此从开始处开始。匹配非数字,然后使用\K重置匹配(这需要结果仅包含您想要的两个数字),匹配两个数字,并在字符串结束前不查找更多数字: ^\D*\K\d{2}(?=\D*$) 另一种选择是使两个数字以非数字分隔的字

我只想在字符串中没有其他数字时查找并捕获两位数字,我的输入:

foo 12 ✅
foo 12 bar ✅
12 baz ✅
9 foo 12 ❌
foo 12 baz 2 ❌
12 2 ❌
我试过这个模式


preg\u match(“`(?您需要检查整个字符串是否包含任何其他数字,因此从开始处开始。匹配非数字,然后使用
\K
重置匹配(这需要结果仅包含您想要的两个数字),匹配两个数字,并在字符串结束前不查找更多数字:

^\D*\K\d{2}(?=\D*$)

另一种选择是使两个数字以非数字分隔的字符串失败,然后匹配两个数字:

*\d\d+\d.*(*F)|\b\d{2}\b
见:

解释

--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \d                       digits (0-9)
--------------------------------------------------------------------------------
  \D+                      non-digits (all but 0-9) (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \d                       digits (0-9)
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (*SKIP)                    backtracking control verb that makes the regex engine
                             "skip" forward to this position on failure and tries 
                             to match again
--------------------------------------------------------------------------------
  (*F)                       fail the current match, proceed to search for a new one
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  \d{2}                    digits (0-9) (2 times)
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
:

if(preg_match('~.*\d\d+\d.**(*F)|\b\d{2}\b~,'foo 12',$match)){echo“foo 12>>>$match[0]\n”}
如果(preg_match('~.*\d\d+\d.*(*SKIP)(*F)|\b\d{2}\b~,'12 baz',$match)){echo“12 baz>>>>$match[0]\n”;}
如果(preg_match('~.*\d\d+\d.*(*SKIP)(*F)|\b\d{2}\b~,'9 foo 12',$match)){echo“9 foo 12>>>>$match[0]\n”}
如果(preg_match('~.*\d\d+\d.*(*SKIP)(*F)|\b\d{2}\b~,'foo 12 baz 2',$match)){echo“foo 12 baz 2>>>>$match[0]\n”}
如果(preg_match('~.*\d\d+\d.*(*SKIP)(*F)|\b\d{2}\b~,'12 2',$match)){echo“12 2>>>>$match[0]\n”;}
结果

foo 12 >>> 12
12 baz >>> 12

你能用
^\D*(\D\D)\D*$
抓取第一个捕获组吗?谢谢,看起来它对我的所有输入都不起作用@mwthrex,一切正常,
\D
匹配换行符。检查