Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

RE: pattern matching: why does the following code evaluate true? (and how do i fix it?)

by Adam (Vicar)
on May 31, 2000 at 02:42 UTC ( [id://15536]=note: print w/replies, xml ) Need Help??


in reply to pattern matching: why does the following code evaluate true? (and how do i fix it?)

Greedy Pattern Matching.
The . matches anything (and more importantly - everything.) {Except \n}

UPDATE: It has been pointed out to me that I am only partly correct. The dot does in fact match anything {Except \n} but it does not behave in a greedy manner. Rather, Perl is saying that the substring ($key2) is found in the other string ($key)... which is ok. I apologize if I confused anyone.

  • Comment on RE: pattern matching: why does the following code evaluate true? (and how do i fix it?)

Replies are listed 'Best First'.
RE: RE: pattern matching: why does the following code evaluate true? (and how do i fix it?)
by takshaka (Friar) on May 31, 2000 at 02:56 UTC
    Well, /^$key2$/, wouldn't match--there's nothing greedy in that regex. But since the pattern isn't anchored it looks for the $key2 pattern anywhere in $key.

    If you're looking for literal periods, do one of the following:

    $key2 = "INITIAL\.LASTNAME,INITIAL2\.LASTNAME2,\."; $key2 = quotemeta "INITIAL.LASTNAME,INITIAL2.LASTNAME2,."; $key =~ /\Q$key2/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found