[英]How to check if regular expression matches the whole string?
I've got a regex that matches stuff like this: asdasd[text]; 我有一个正则表达式,可以匹配这样的东西:asdasd [text]; it works fine one step at a time, but if a have something like: 一次只执行一次即可,但是如果有类似以下内容的话:
$badinput="add[2,5,525];print['peron'];print['asd','cgfg];time;print['iudofiusdoif'];"; #time should not be matched
this is the code for now: 这是现在的代码:
verify($badinput);
sub verify
{
#returns 1 if it's ok, or the text that breaks the match
my $inp = pop;
if ($inp =~ /^(?:\w{2,6}\[(?<!\\\[).*?\](?<!\\\]);)+$/s)
{
return 1;
}else{
return 0; #should be the text that breaks the match or the char number
};
}
It returns 1 no matter what if the first instruction matches. 无论第一个指令是否匹配,它都会返回1。 How do i solve this? 我该如何解决?
One way. 单程。 My regular expression it's similar to yours but without look-behind. 我的正则表达式与您的正则表达式相似,但没有后顾之忧。
An example. 一个例子。 Content of script.pl
: script.pl
内容:
#! /usr/bin/perl
use warnings;
use strict;
while ( <DATA> ) {
chomp;
printf qq|%s ==> %s\n|, $_, ( m/^(\w{2,6}\[[^]]*\];)+$/ ) ? q|ok| : q|not ok|;
}
__DATA__
add[2,5,525];print['peron'];print['asd','cgfg];time;print['iudofiusdoif'];
add[2,5,525];print['peron'];print['asd','cgfg];print['iudofiusdoif'];
Run it like: 像这样运行:
perl script.pl
With following output: 具有以下输出:
add[2,5,525];print['peron'];print['asd','cgfg];time;print['iudofiusdoif']; ==> not ok
add[2,5,525];print['peron'];print['asd','cgfg];print['iudofiusdoif']; ==> ok
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.