i'm working on simple lexical analyser project , i'm using array store regex patterns strings.
i test each regex pattern individually make sure correct output when checking line of file.
the problem is, once stored these strings in array started getting several errors. when searching strings start backslash \
.
this perl code
#!/usr/bin/perl @patterns = ("\\begin", "\\end", "{", "}", "<\d+(\.\d+)?>", "<p>", "<\\p>", ".*", "%%+", "<%", "%>") ; print "enter text: "; chomp( $input = <> ); print test(); sub test() { $arrsize = @patterns; ( $i = 0; $i < $arrsize; $i++ ) { if ( $input =~ /$patterns[$i]/gi ) { print "good input\n"; } else { print "bad input\n"; } } }
this above test file read lines manually type in check regex expression , give me input if matches or bad input if doesn't.
perl continues skip on backslashes no matter how use in each string.
i'm using default perl v5.18.2 installed ubuntu 14.04.
strings in double quotes "interpolated" in perl. backslash has special meaning here. if want store regular expressions in array, it's better use qr//
construct:
my @patterns = ( qr/\\begin/, qr/\\end/, qr/{/, qr/}/, qr/<\d+(\.\d+)?>/, qr/<p>/, qr(</p>), # assumed html/php, replaced \p. qr/.*/, qr/%%+/, qr/<%/, qr/%>/, ) ;
you should use warnings, warn against mistakes made:
unrecognized escape \d passed through @ /home/choroba/1.pl line 5. unrecognized escape \d passed through @ /home/choroba/1.pl line 5. main::test() called check prototype @ /home/choroba/1.pl line 10.
Comments
Post a Comment