php preg_match()在多个分隔符中

  • 本文关键字:分隔符 preg match php php
  • 更新时间 :
  • 英文 :


我有字符串:

$data = "012.03.AB";

我想要preg_match for:
012=数字&3位
03=数字&2位
AB=字母表&2位

这是我的代码:

$data = "012.03.AB";
preg_match("/[0-9]{3}.[0-9]{2}|.([A-Z]{2})/", $data);

但不工作

如果我正确理解你,你的问题是:

  • 您的模式中间有一个松散的|
  • 您的第二个子模式设置为3位数,但您说它应该是2数字
  • (可能是故意的,也可能不是故意的(你的模式没有固定

if (preg_match('#^([0-9]{3}).([0-9]{2}).([A-Z]{2})$#D', $data, $result) === 1) {
// $data matched; you can use the segments as:
$first  = (int) $result[1];
$second = (int) $result[2];
$third  =       $result[3];
} else {
// $data did not match
}

如果这两个字符也允许小写,您可能需要在末尾添加i标志。

最新更新