PHP Regex将递增ID与可选空格匹配

  • 本文关键字:空格 ID Regex PHP php regex
  • 更新时间 :
  • 英文 :


尝试在字符串中查找增量,如预期用途(银行)等。

我尝试创建一个匹配123456 ...123 456 ...的正则表达式。

我收到的预定目的线内爆而没有空位。所以我会得到这样的增量FooBar 123456 BazFooBar 123456Baz或CCD_ 5。

增量从15或16开始。示例:150000000001160000000001

我得到的:(^s*|[^0-9])((15|16)(?:ds?){10})([^0-9]|s*$)

Pattern break down:
#
// Begin of string (perhaps empty spaces) OR no digit.
(^s*|[^0-9])
// Prefix "15" or "16" and 10 digits (perhaps empty spaces between).
((15|16)(?:ds?){10})

// Not following by digit OR (perhaps empty spaces between) end of string.
([^0-9]|s*$)

#

我遇到的第一个问题是(?:ds?)总是选择后面的空白。第二个问题是,我在开头和结尾的尝试都没有真正奏效。

主要部分是((15|16)(?:ds?){10})。但这也会在换行后继续选择,这是不应该的。这就是为什么我试图用";而不是0-9或结束|行的开始;。

我在regex101.com上测试这个

应该匹配的示例

-- no empty space at start of line, and one or more spaces between, and none-number before|after
150000000001 150000000001  150000000001 150000000001 d 150000000001d 150000000001 d150000000001d
-- empty space before and in between
150 000 000 001
15 00 00 00 00 01 --not a must tho
150000000001    -- just a string of the increment only
160000000001    -- just a string of the increment only

新线问题-

-- the new line problem
Foo Bar160000000
001 Baz

选择

160000000
001

这是不应该的。

如何适当地限制开头和结尾?

感谢您抽出时间

您可以使用

/(?<!d)1[56]d(?:h?d){9}(?!d)/

正则表达式匹配:

  • (?<!d)-左边的数字边界,左边不允许有任何数字
  • 1-一个1字符
  • [56]-56字符
  • d-任意数字
  • (?:h?d){9}-可选水平空白和任意一位数字出现9次
  • (?!d)-右侧数字边界,右侧不允许立即出现任何数字

最新更新