除包含“000”的字符串外的任何数字字符串

Any numeric string except the string containing '000'

如何匹配除000以外的所有数字。也就是说,

001234567502344001233400122300 is fine.
0123456750023440012334012230 is fine.
000123456750234400123340012230 is not fine.
001234567502344000123340012230 is not fine.
0012345675023440012334001223000 is not fine.
00123456750234400012334001223000 is not fine.
001002003004005006 is fine.
001 id fine
10 is fine.
01 is fine.
000 is not fine.

我应该使用负前瞻还是以下技术:

/(()|()|())/g

你想要

$string !~ /000/

测试:

$ perl -nle'printf "%s is %s\n", $_, !/000/ ? "fine" : "not fine"' <<'.'
001234567502344001233400122300
0123456750023440012334012230
000123456750234400123340012230
001234567502344000123340012230
0012345675023440012334001223000
00123456750234400012334001223000
001002003004005006
001
10
01
000
.
001234567502344001233400122300 is fine
0123456750023440012334012230 is fine
000123456750234400123340012230 is not fine
001234567502344000123340012230 is not fine
0012345675023440012334001223000 is not fine
00123456750234400012334001223000 is not fine
001002003004005006 is fine
001 is fine
10 is fine
01 is fine
000 is not fine

如果这是更大模式的一部分,您要确保每个位置都不是 000 的开始。

(?:(?!000).)*

例如,

/^(?:(?!000).)*\z/

例如,

my @safe_numbers = $string_with_multiple_numbers =~ /\b(?:(?!000)\d)*\b/g;

您可以使用

^(?!\d*000)\d+$

regex demo and the Regulex graph:

详情

  • ^ - 字符串的开头
  • (?!\d*000) - 在字符串开始之后,不能有任何 0+ 数字后跟 000 子字符串
  • \d+ - 1+ 位数
  • $ - 字符串结尾。