hmbscully has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to restrict the valid values that the user inputs to letters, numbers, spaces, opostrophes, periods and dashes.

I'm trying this subsitution to get rid of any offenders:
s/[^A-Za-z\d\s\-\.\']//g; with a test value of D3E' C@$o*r(-A[].Mick7
that should return D3E' Cor-A.Mick7
but instead is returning
D3E' Cr-A.Mick7

What am I not understanding is what allows it to remove 'o' when I don't want it to? I figure it has something to do with the order in which things are matched and something about the 'o' being surrounded by offending characters, but I'm at a bit of a loss.
Thanks!

UPDATE: *d'oh* I had commented out the use warnings, use strict earlier because of some other issue and forgot to put them back in when I did more work.
Glad to know it was just some simple oversight rather than a complete lack of understanding on my part. Thanks!


I learn more and more about less and less until eventually I know everything about nothing.

Replies are listed 'Best First'.
Re: Quickie Question: RegEx matching problem
by liverpole (Monsignor) on Aug 18, 2006 at 14:39 UTC
    Hi hmbscully,

    It looks like the problem you're having is the $o in your string; it is trying to substitute in the value of the variable $o.  Try escaping the '$' and the '@' with:  D3E' C\@\$o*r(-A[].Mick.

    By the way, if you had used:

    use strict; use warnings;

    at the top of your program, it would have warned you of the fact that $o was undefined.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Quickie Question: RegEx matching problem
by Fletch (Bishop) on Aug 18, 2006 at 14:39 UTC

    You probably don't have use strict enabled and you have your test value in double quotes so the $o is being interpolated silently into an empty string.

Re: Quickie Question: RegEx matching problem
by chargrill (Parson) on Aug 18, 2006 at 14:41 UTC

    This works for me:

    $_ = q{D3E' C@$o*r(-A[].Mick7}; s/[^A-Za-z\d\s\-.\']//g; print "$_\n";

    I suspect you're quoting your test string with double quotes, and the $o is interpolating into the string - it's most likely undefined, and thus never shows up in your output.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}