Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Regex matching after ASCII characters

by Anonymous Monk
on Oct 05, 2011 at 21:11 UTC ( [id://929881]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex matching after ASCII characters
in thread Regex matching after ASCII characters

Thanks for your quick response, I tried your code and am getting the same false positives.

Example: $prompt_host = "BR|GR", filenames such as: YUGRABCKFI01-1.1.1.1_2011.10.04.00.00.00.txt and GRREPCCOBE10-1.1.1.1_2011.10.04.00.00.00.txt are erroneously being pushed. Here is how I implemented your code:

while (my $file = readdir(DIR)) { if($file =~ m{ \A [A-Z0-9]{8} $prompt_host }xms) { push(@traffic_file_list, $file); } }

Replies are listed 'Best First'.
Re^3: Regex matching after ASCII characters
by elTriberium (Friar) on Oct 05, 2011 at 21:36 UTC
    The problem with your code is the "BR|GR". Perl will now match any of these 2:
    \A [A-Z0-9]{8} BR
    OR:
    GR
    The second one matches. The solution would be to set $prompt_host to:
    $prompt_host = (?:BR|GR)

      Perfect, thank you guys for your help.

Re^3: Regex matching after ASCII characters
by AnomalousMonk (Archbishop) on Oct 05, 2011 at 22:18 UTC

    And with some sanity checking:

    >perl -wMstrict -le "my @t = ( 'YUGRABCKFI01-1.1.1.1_2011.10.04.00.00.00.txt', 'GRREPCCOBE10-1.1.1.1_2011.10.04.00.00.00.txt', ); ;; ENTRY: for my $entry ('BR|RG', 'FI', '(?{ `rm -R *` })', '++') { my $rx = eval { qr{ \A [A-Z]{6} [A-Z\d]{2} (?: $entry) }xms }; if ($@) { print qq{user entered '$entry' is evil: $@}; next ENTRY; } for my $t (@t, @ARGV) { printf qq{%7s %-3smatch with '$t' \n}, qq{'$entry'}, $t =~ $rx ? '' : 'NO' ; } } " "STTLWA02RG01_2011-10-05.00.00.00.txt" 'BR|RG' NO match with 'YUGRABCKFI01-1.1.1.1_2011.10.04.00.00.00.txt' 'BR|RG' NO match with 'GRREPCCOBE10-1.1.1.1_2011.10.04.00.00.00.txt' 'BR|RG' match with 'STTLWA02RG01_2011-10-05.00.00.00.txt' 'FI' match with 'YUGRABCKFI01-1.1.1.1_2011.10.04.00.00.00.txt' 'FI' NO match with 'GRREPCCOBE10-1.1.1.1_2011.10.04.00.00.00.txt' 'FI' NO match with 'STTLWA02RG01_2011-10-05.00.00.00.txt' user entered '(?{ `rm -R *` })' is evil: Eval-group not allowed at runtime, use re 'eval' in regex m/ \A [A-Z]{6} [A-Z\d]{2} (?: (?{ `rm -R *` })) / at ... user entered '++' is evil: Quantifier follows nothing in regex; marked by <-- HERE in m/ \A [A-Z]{6} [A-Z\d]{2} (?: + <-- HERE +) / at ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://929881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found