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

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.

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

    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