in reply to A Regex to identify Regex's / Compiling a regex

$constraint =~ m@ ^\s* # skip all whitespace at beginning ( # start capturing /.+/ # match a '/' any number of anycha +r '/' | # or m # char 'm' (start of a match) (.) # capture the delimiter .+ # skip all the stuff \2 # upto the second occurance of the + delimiter ) #end capture [cgimosx]* # match zero or more match options \s*$ # skip white space @x

Basically, the regex validates regex of either /????/ or m/?????/cgimosx form.

I think your problem is that when the \ is at the end, it is escaping the the final forward slash and that won't match the regex validating regex. Try using the m!! form with a different delimiter. Should help.


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Replies are listed 'Best First'.
Re: Re: A Regex to identify Regex's / Compiling a regex
by knowmad (Monk) on Sep 19, 2002 at 22:01 UTC
    Ahhh, now I see! Thanks for editing that regex for me. I like your explanation of why using a \\ at the end fails. However, if the regex did not match, then a compile error would not occur as it would not enter the if statement.

    Following your suggestion of using the m!! format works. So the problem lies in how the regex is getting compiled. I did a bit more testing and discovered that using the \\ fails when it is next to a \/ combination (on either side). It fails if I use the // or m// style. At this point it's mostly a matter of curiousity but an interesting anomaly within either the eval function or the Perl regex engine.

    Thanks, William

      At this point it's mostly a matter of curiousity but an interesting anomaly within either the eval function or the Perl regex engine.
      It's not. It's just that you're trying to match a backslash, for which you need a double backslash in the regex. But to achieve that, you need to type 4 backslashes in a singlequotish string. It's what's in the string that matters for the regex engine, not what's in your source code.

      A backslash only disappears in such a string, if it's in front of a backlslash or a string delimiter (here "'").

      I don't think there is any mystery here. Although the regexes are originally within single quotes and therefor not interpolated, in the eval they are being concatenated with the rest of the sub statement. The eval see's it just as a string. When the interpreter tries to evaluate the combined string the final delimiter of the match is escaped, hence the error, Search pattern not terminated.

      It get's into the if statement ok, but its when its concatenated prior to evaluation where the problem arises.

      That's why using m!! cures the problem.

      I'll add... I think, cos I'm not great with regexes.


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!