in reply to Re: Re: Regular Expression gotchas ?
in thread Regular Expression gotchas ?

Strangely enough, you've hit a regular expression gotcha.

The empty regular expression has a special meaning: try again the last regular expression that successfully matched. What is perhaps less obvious is that perl checks for an empty expression after interpolating variables, such as the \Q$exp_sender\E in your example.

Here's a snippet to illustrate:

"test" =~ /t/; # successful match my $var = ""; print "matched 't'\n" if "t" =~ /\Q$var\E/; print "matched 'u'\n" if "u" =~ /\Q$var\E/;

A couple of alternative approaches, depending on what you're trying to achieve:

print "'$signer' is not '$exp_sender'\n" unless $signer eq $exp_sender; print "'$signer' is not '$exp_sender'\n" unless index($signer, $exp_sender) >= 0; print "'$signer' is not '$exp_sender'\n" unless $exp_sender eq '' || $signer eq $exp_sender;

Hugo

Replies are listed 'Best First'.
Re: Re: Regular Expression gotchas ?
by the_Don (Scribe) on Feb 18, 2003 at 22:18 UTC

    First of all - Thank you very much.

    I am wondering if there is a way to clear out the $& value that holds the value of the last successful match (which is what I think is being used, if its one of the other perl vars, please correct me).

    If there is not a way around it, I will have to change more of the original code, but I was hoping that I could clear and or reset the perl var.

    PS. I am aware that $& is described as READONLY, but there seems to be tricks for everything, I am hoping there is a trick for this as well. i.e. a match that will effectively clear the value?

    the_Don
    ...making offers others can't rufuse.

      It isn't $&, it is an invisible "what pattern did the last successful match use?" saved for just this purpose.

      I think the easiest way to clear it out (if I guess correctly what you mean by that) is to find a pattern that acts like the empty pattern would were it not special-cased. The simplest one I know is the empty lookahead:

      "" =~ /(?=)/;

      That'll always match, and do so pretty quickly; once matched, it'll be remembered as the last successful match, so an empty pattern will use it and always match.

      But be warned that this is an ugly workaround that'll make your code harder to write in the first place, and harder to maintain in the future. Better by far would be to avoid the problem in the first place.

      Since the original check, $signer =~ m/\Q$exp_sender\E/, is identical to:

      index($signer, $exp_sender) > 0
      except for the special-case behaviour on the empty pattern, I suspect the latter is what you actually want.

      Hugo