On Mon, Jan 06, 2003 at 11:40:08PM -0200, andrej hocevar wrote:
> I've been playing around with these two REs in Perl with no
> success. Can anyone tell me what's wrong?
> 
> This works:
> !~ /^(?:red|blue)$/

        $var !~ /<expression>/ ==> var as no match for expression

        /^<expression>$/ ==> match expression at ^beginning and
        ending$ of line

        /(<expression>)/ ==> match what's inside the (parens) as a
        whole unit

        /(?:<expression>)/ ==> match (paren) expression without
        remembering the match for a $1 or $2 or $3... replacement

        /<expr1>|<expr2>/ ==> match either expr1 or expr2

so

        $var !~ /^(?:red|blue)$/

        !~       is false when
        /        matching
        ^        at the beginning of the line
        (|)      one of the alternatives
(       :?       without having to remember the sub-match )
        red|blue either "red" or "blue"
        $        at the end of the line (right before newline, if any)

to reverse it, change !~ (false when matching) to =~ (true when
matching).

> and will match everything except any of the two fixed strings
> "red" or "blue" or any combination thereof. "black" matches,
> as does "blu", because it's neither red nor blue.

slight rewording needed -- "black" comes back as true BECAUSE IT
DOESN'T MATCH, and that's what you're looking for with !~. it
might seem like a small distinction until you realize how easy
it is to be misled! :)

=~ match                $var =~ /expr/
!~ don't match      ! ( $var =~ /expr/ )

> Then I wanted to convert the above expression to a "positive"
> match, like this:
> =~ /^[^(?:red|blue)]$/
> 
> which fails. Why is this? Is there no way of saying "neither/nor",
> just "either/or"?

using brackets, /[something]/ will match any s, o, m, e, t, h,
i, n, or g.

all you needed to do was change !~ (doesn't match) to =~ (does
match) and you should be done.

        $goodsign = ( $politics !~ /republican|democrat/i );
        $american_phone =~ /\b(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)\b/;

-- 
I use Debian/GNU Linux version 3.0;
Linux server 2.2.17 #1 Sun Jun 25 09:24:41 EST 2000 i586 unknown
 
DEBIAN NEWBIE TIP #56 from Vineet Kumar <[EMAIL PROTECTED]>
:
Troubled by DOS-FORMAT OR MAC-FORMAT TEXT FILES? Here's another
way to deal with those troublesome ^M characters: a simple
        tr -d '\015'  < dos.file  > reg.file
should do the trick.  While we're on the subject, a Mac file
can be converted with
        tr '\015' '\012'  < mac.file  > reg.file
You can do all your CR/LF translations with tr as long as you
can remember that macs use CRs, *nices use LFs, and DOS uses
CR+LF.

Also see http://newbieDoc.sourceForge.net/ ...


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to