How to remove capture from arbitrarily nested subgroups in a Perl regex string? I would like to embed any regular expression in a wrapping expression that captures the sub-regression of both the whole entity and the statically known subsequent groups. Do I need to convert the regular expression string manually to use all non-capturing groups (?:) (and hopefully I won't mess up), or is there a regular expression mechanism or Perl library that provides this?
# How do I 'flatten' $regex to protect $2 and $3?
Addendum: I vaguely know $& , $` and $' , and I was advised to avoid them if possible, and I do not have access to ${^PREMATCH} , ${^MATCH} and ${^POSTMATCH} in my Perl 5.8 environment. The above example can be divided into 2/3 pieces using such methods, and more complex real cases can manually iterate, but I think I would like, if possible, a general solution.
Accepted answer: What I want, and surprisingly (at least for me), is not an encapsulating group that makes its contents opaque, so subsequent positional backlinks see the contents as a whole and name links are shortened. gbacon has a potentially useful solution for Perl 5.10+ and FM shows a manual iterative mechanism for any version that can perform the same effect in certain cases, but j_random_hacker calls it that there is no real language mechanism for encapsulating subexpressions.
regex perl
Jeff
source share