in reply to Re: Regex help
in thread Regex help

I don't think so. Did you test that notion? Suggestion: try moving the "\$" to the last position in the class.

See characters in a character-class in "Mastering Regular Expressions" and Friedl's explanation that character-class-metacharacters are NOT the same as metacharacters outside a character class (pp 9-10). He also alludes to the differences elsewhere in the text. Very little is interpolated inside a character class (with the obvious exceptions of the likes of "-" or an initial "^" negation).

However, returning to OP's conundrum: it does NOT appear to be quite as stated. Rather, it appears to me that "Hello there0" matches his posted regex, while removing the preceding "\" before the "$" causes it to fail.

Now, the distraction of a very nice summer day (one of very few, thusfar) discourages me from feeling that the following is an adequate answer, but /me thinks "hello there0" passes his regex because his regex does not include decimal digits (see my line 14).

However, I must admit, I don't understand why, when using my line 11 rather than 14, the output is as it is: "hello there0" is not okay, but "hello there$ now" is. Brighter minds; wiser heads, pray edify!

Update: graff has. See his correct analysis below and everybody's above. Apologies to everybody and to OP. Balance of this post allowed to stand for whatever value (as an object lesson in incomplete testing/analysis) it may have.

#!/usr/bin/perl use strict; use warnings; # 783199 my @data = <DATA>; for my $line (@data) { chomp $line; # if ( $line =~ /[<>@#\$%^&*()_+=|\{\}\[\]\/\\]/ ) { # OP's char + class ie "escaped the $ sign" # if ( $line =~ /[<>@#%^&*()_+=|\{\}\[\]\/\\]\$/ ) { # OP's char + class ie "escaped the $ sign" with \$ moved to end of class # if ( $line =~ /[<>@#$%^&*()_+=|\{\}\[\]\/\\]/) { # $ sign no +t backslashed # if ( $line =~ /[<>@#\$%^&*()_+=|{}\]\/\\]/ ) { # unnecessa +ry backslashes removed if ( $line =~ /[0-9<>@#\$%^&*()_+=|{}[\]\/\\]/ ) { # rul +e out decimal digits print "\$line: $line \t is not okay \n"; }else { print "\$line: $line \t is okay \n"; } } =head $ sign in char class preceded by a backslash $line: hello there0 is okay $line: hello there is okay $line: hello there$ now is not okay $line: hello there $ now is not okay $line: hello there [ is not okay $line: by [rights] this should fail. is not okay $ sign in char class NOT preceded by a backslash $line: hello there0 is not okay $line: hello there is okay $line: hello there$ now is okay $line: hello there $ now is okay $line: hello there [ is not okay $line: by [rights] this should fail. is not okay Unnecessary backslashes removed: $line: hello there0 is okay $line: hello there is okay $line: hello there$ now is not okay $line: hello there $ now is not okay $line: hello there [ is okay $line: by [rights] this should fail. is not okay and with Decimal Digits rejected: $line: hello there0 is not okay $line: hello there is okay $line: hello there$ now is not okay $line: hello there $ now is not okay $line: hello there [ is not okay $line: by [rights] this should fail. is not okay =cut __DATA__ hello there0 hello there hello there$ now hello there $ now hello there [ by [rights] this should fail.

Replies are listed 'Best First'.
Re^3: Regex help
by graff (Chancellor) on Jul 25, 2009 at 21:17 UTC
    Sorry, but it looks to me like the first reply above got it right. The OP said that if the value of $input was "hello there0", and the regex did not have a backslash in front of the "$" in the character class, the regex would match and yield "not okay" as the output. But this goes against the intent of the regex, which is to match only on the particular set of non-alphanumeric characters -- including "$" and "%".

    The OP figured out that putting backslash in front of "$" would make the regex work as intended, but did not understand why, and everybody gave the correct explanation: without the backslash, you get an interpolation of the variable "$%", and its value turns out to be zero. Try this (NB: this uses bash shell style quoting):

    perl -le '$regex = qr:[<>@#$%^&*()_+=|\{\}\[\]\/\\]:; print "$%"; print "$regex";'
    You'll see a zero in both lines of output.