无论如何,我是否可以避免将以下规则中的 9 位数字与我拥有的正则表达式相匹配?
我有以下正则表达式,它遵循以下规则,
(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
规则:
- 如果“X 或 x”在前,则不应检测到 9 位订单号 号码。 (工作正常)
- 9 位数字、非数字字符或空格(最多 3 个) 数字之间也应该匹配。 (工作正常)
下面是正则表达式演示,显示它与上述规则匹配数字。
https://regex101.com/r/mrGcvp/1
现在,正则表达式模式不应匹配遵循上述规则的 9 位数字,如果符合以下排除规则。
排除规则
对于以下规则,数字不应完全匹配。
- 如果以数字“9”开头的数字
- 如果在位置 1 – 3 中为数字“666”。
- 如果在位置 1 – 3 中为数字“000”。
- 如果在位置 4 – 5 中为数字“00”。
- 如果数字“ 0000”位于位置 6 – 9
I have the below regex which follows the following rules,
(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
Rules:
- 9 digit Order numbers should not get detected if 'X or x' precedes
the number. (WORKING FINE) - 9 digit numbers, Non-numeric characters or whitespaces (up to 3) in
between numbers should also get matched. (WORKING FINE)
Below is regex demo which shows it matches the numbers with the above rules.
https://regex101.com/r/mrGcvp/1
Now, the regex pattern should not match the 9 digit numbers following the above rules if it comes under the below rules of exclusion.
Rules of exclusion,
The number should not be matched at all for the following rules.
- If the number beginning with the number “9”
- If the number “666” in positions 1 – 3.
- If the number “000” in positions 1 – 3.
- If the number “00” in positions 4 – 5.
- if the number “0000” in positions 6 – 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
查看正则表达式演示。
添加的部分是
(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D* ){5}0(?:\D*0){3})
如果紧邻当前位置的右侧,就在(? 和
(?=(?:[._ –-]*\d){9})
检查,有9|
- 一个9
数字,或66\D*6|
-66
、零个或多个非数字、6
或00\D* 0|
-00
,零个或多个非数字、0
或(?:\d\D*){3}0\D*0|
- 出现 3 次数字,然后出现 0 次或多次非数字,然后是0
、零个或多个非数字、0
或(?:\d\D*){5}0( ?:\D*0){3})
- 出现五次一个数字和零个或多个非数字0
,然后出现 3 次零个或多个非数字,后跟一个0
字符。请注意,我使用
\D*
而不是[._ –-]*
,这在这里应该足够了,但如果你想让它更精确,你可以替换每个 < code>\D* 与[._ –-]*
。You can use
See the regex demo.
The added part is
(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})
and it fails the match if, immediately to the right of the current location, right after the(?<!x)
and(?=(?:[._ –-]*\d){9})
checks, there is9|
- a9
digit, or66\D*6|
-66
, zero or more non-digits,6
, or00\D*0|
-00
, zero or more non-digits,0
, or(?:\d\D*){3}0\D*0|
- three occurrences of a digit and then zero or more non-digits, and then a0
, zero or more non-digits,0
, or(?:\d\D*){5}0(?:\D*0){3})
- five occurrences of a digit and zero or more non-digits,0
, and then three occurrences of zero or more non-digits followed with a0
char.Note I used
\D*
instead of[._ –-]*
that should be enough here, but if you want to make it more precise, you may replace each\D*
with[._ –-]*
.您可以使用此正则表达式:
RegEx 演示
为了强制执行所有规则,我们有一个否定的前瞻
:以下:
9
:不以9
开头666
:不以666
开头000
:不以000
开头。{3}00
:不允许在位置 4 中使用00
-5.{5}0000
:不允许在位置 6-9 出现0000
You may use this regex:
RegEx Demo
To enforce all rule we have a negative lookahead:
That does following:
9
: Doesn't start with9
666
: Doesn't start with666
000
: Doesn't start with000
.{3}00
: Doesn't allow00
in position 4-5.{5}0000
: Doesn't allow0000
in position 6-9