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

What is the correct idiom for a character class containing all chars except whitespace and the letter A? The obvious
[^A\S]
doesn't work, for reasons I don't entirely understand.

I can think of workarounds on my own, but I'd like to know the Right Way to do this. Thanks.

Replies are listed 'Best First'.
Re: Mixing builtin char-classes and negation
by broquaint (Abbot) on Jun 16, 2003 at 19:09 UTC
    Lowercase that \S, this will result in a negated character class containing an 'A' and whitespace (as defined by \s) e.g
    my $cc = qr/^[^A\s]+$/; print "foo" =~ $cc ? "yep" : "nope"; print "foA" =~ $cc ? "yep" : "nope"; print "fo " =~ $cc ? "yep" : "nope"; __output__ yep nope nope
    See. perlre for more info.
    HTH

    _________
    broquaint

Re: Mixing builtin char-classes and negation
by sauoq (Abbot) on Jun 16, 2003 at 20:52 UTC
    The obvious
    [^A\S]
    doesn't work, for reasons I don't entirely understand.

    It doesn't work because you are negating a character class that has already been negated. The class, \S, means anything but whitespace. Negating that results in "any whitespace".

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Mixing builtin char-classes and negation
by halley (Prior) on Jun 16, 2003 at 19:10 UTC

    perlre in the POSIX section would lead me to believe [^A[:space:]] would be the desired syntax. A test is in order...

    % perl $a = 'this is a test'; $a =~ s/[^i[:space:]]/-/g; print $a,$/;
    --i- i- - ----

    --
    [ e d @ h a l l e y . c c ]

Re: Mixing builtin char-classes and negation
by traveler (Parson) on Jun 16, 2003 at 20:52 UTC
    I think you want something like this:
    $a = "FATHER ANDY"; $a =~ s/[^(A\s)]/-/g; print "$a\n";
    gives
    -A---- A---

    HTH, --traveler

      He didn't say anything about parentheses.

      $a = "(woozle)"; $a =~ s/[^(A\s)]/-/g; print "$a\n";
      gives
      (------)

      -sauoq
      "My two cents aren't worth a dime.";
      
        print "I will not copy stuff from other programs without fully testin +g it" while 1;