in reply to Re^3: Regex: return the pattern instead of the actual match
in thread Regex: return the pattern instead of the actual match

Really? How does that work? How do you re-construct the subpattern from the (not necessarely unique) label you've given it? And why can't you do the same magical trick with the unique numbered match?
  • Comment on Re^4: Regex: return the pattern instead of the actual match

Replies are listed 'Best First'.
Re^5: Regex: return the pattern instead of the actual match
by LanX (Saint) on Sep 16, 2011 at 14:40 UTC
    >Really? How does that work?

    Re: Regex: return the pattern instead of the actual match

    > And why can't you do the same magical trick with the unique numbered match?

    you might need other groupings which are not necessarily interesting.

    The template system I used offers full flexibility. If you wanna do this with numbered matches you need to parse the template.

    Cheers Rolf

      Oh, sure, that works. But that isn't very much different from using:
      /(foo\d+)|(bar\S+?)/
      and checking which of $1 or $2 is defined.

      It requires some out-of-bound data that maps to capture group identifier to sub-pattern, and this out-of-bound data needs to be set up when creating the pattern (you can't use this trick if the pattern is given, unless you deconstruct and reconstruct the pattern).

      And then, instead of using captures, one may use marks:

      /foo\d+(*:foo)|bar\S+(*:bar)/
      and consult $REGMARK afterwards.
        > Oh, sure, that works. But that isn't very much different from using:

        > /(foo\d+)|(bar\S+?)/

        > and checking which of $1 or $2 is defined.

        For my approach it's much easier to construct more complicated regexes like

        /pre(§FOO§|§BAR§)post/

        In this rather simple example FOO and BAR are $2 and $3!

        I think the idea of the OP is to use regexes as rules and to efficiently check which rules apply.

        My initial approach would be to further investigate the matched string for each rule.

        But the named match approach has many benefitsecause I will in any way need to have a hash holding the rules.

        > and consult $REGMARK afterwards.

        experimental feature! :(

        Cheers Rolf