0

次のようなものに一致する正規表現があります: asdasd[text]; 一度に 1 ステップずつ正常に動作しますが、次のような場合:

$badinput="add[2,5,525];print['peron'];print['asd','cgfg];time;print['iudofiusdoif'];"; #time should not be matched

これは今のところコードです:

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
    };
}

最初の命令が一致した場合は、何があっても 1 を返します。どうすればこれを解決できますか?

4

2 に答える 2

2
sub verify
{
  return ($_[0] =~ m/^((?:\w{2,6}\[[^\]]*\];)+)$/)? 1 : 0;
}

このコードをここでテストします。

于 2012-08-17T23:51:21.990 に答える
2

一方通行。私の正規表現はあなたのものに似ていますが、後読みはありません。

例。の内容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'];

次のように実行します。

perl script.pl

次の出力で:

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
于 2012-08-17T22:27:31.460 に答える