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/