我怎样才能写出正则表达式
每当有 0000 时,后面就应该有 1111,例如:
00101011000011111111001111010 -> correct
0000110 -> incorect
11110 -> correct
谢谢你的帮助
如果您使用 Perl,则可以使用零宽度负前瞻断言:
#!/usr/bin/perl
use strict; use warnings;
my @strings = qw(
00101011000011111111001111010
00001111000011
0000110
11110
);
my $re = qr/0000(?!1111)/;
for my $s ( @strings ) {
my $result = $s =~ $re ? 'incorrect' : 'correct';
print "$s -> $result\n";
}
如果存在字符串,则模式匹配0000
not随后至少有四个1
s。因此,匹配表明字符串不正确。
Output:
C:\Temp> s
00101011000011111111001111010 -> correct
00001111000011 -> incorrect
0000110 -> incorrect
11110 -> correct
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)