如何使用正则表达式检查四个零的每个子字符串是否后跟至少四个零? [英] How can I check if every substring of four zeros is followed by at least four ones using regular expressions?
本文介绍了如何使用正则表达式检查四个零的每个子字符串是否后跟至少四个零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何写正则表达式,其中每当有 0000 时,后面应该有 1111,例如:
How can I write regular expression in which whenever there is 0000 there should be 1111 after this for example:
00101011000011111111001111010 -> correct
0000110 -> incorect
11110 -> correct
感谢您的帮助
推荐答案
如果您使用的是 Perl,则可以使用 零宽度负前瞻断言:
If you are using Perl, you can use a zero-width negative-lookahead assertion:
#!/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
的字符串,则模式匹配.因此,匹配表示不正确的字符串.
The pattern matches if there is a string of 0000
not followed by at least four 1
s. So, a match indicates an incorrect string.
输出:
C:\Temp> s
00101011000011111111001111010 -> correct
00001111000011 -> incorrect
0000110 -> incorrect
11110 -> correct
这篇关于如何使用正则表达式检查四个零的每个子字符串是否后跟至少四个零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文