正则表达式 - 检测数字不断变化的字符串

RegExp - Detect string with a changing number

我有这样一个字符串:

A customer with the id '2589' already exists.

数字可以改变,其他字每次都一样。我如何检测是否是这条消息,不管号码是多少?

所以我在想这样的事情:

$str = 'A customer with the id '2589' already exists.';
preg_match_all($pattern, $str, $result);

if ($result[0] == 'A' && $result[1] == 'customer' && $result[2] == 'with')...

有人可以帮忙吗?

谢谢

A customer with the id '(\d+)' already exists\.

使用\d检测数,+为1个或多个

解释如下:https://regex101.com/r/3lEsI4/2

如果您需要设置此号码的最小长度,只需使用 {4,}(最少 4 位数字)

如果你想设置最大长度使用{,6}(最多6位)

如果您需要同时设置它们,请使用 {4,6}(最少 4 位和最多 6 位数字)

例如:

A customer with the id '(\d{4,6})' already exists\.

所以

$pattern = "/A customer with the id '(\d+)' already exists\./";

怎么样

$str = 'A customer with the id '2589' already exists.';
$isAMatch = preg_match("/A customer with the id \'(\d)+\' already exists\./", $str);

为什么不只搜索 "A customer with the number" 和 "already exists"?如果两者都找到,那么您可能正在查看这些消息之一