I've got a regex that matches stuff like this: 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. 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
:
#! /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
sub verify
{
return ($_[0] =~ m/^((?:\w{2,6}\[[^\]]*\];)+)$/)? 1 : 0;
}
Test this code here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.