除包含'000'的字符串之外的任何数字字符串

问题描述 投票:1回答:2

如何匹配除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.

我应该使用负面Lookaheads或以下技术:

/(()|()|())/g
regex pcre regex-negation regex-lookarounds regex-group
2个回答
1
投票

你可以用

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

查看regex demoRegulex graph

enter image description here

细节

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

1
投票

你要

$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;
© www.soinside.com 2019 - 2024. All rights reserved.