Forever-Young / mrab-regex-hg

Automatically exported from code.google.com/p/mrab-regex-hg
0 stars 0 forks source link

repeated expressions (?1) - already supported? #72

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
I recently noticed a feature, which appears to be very useful, namely the 
repeated (sub)expressions, which should have the same syntax as the recursive 
expressions:
http://www.asiteaboutnothing.net/regexp/regex-disambiguation.html#repeat

I'd like to ask, whether it isn't actually already supported in regex as a part 
of the recursive patterns implementation.

It seems so:

>>> [m.group() for m in regex.finditer(r"([ab])-(?1)", 
"XXa-bXa-aXb-bXb-aXb-cXXXX")]
['a-b', 'a-a', 'b-b', 'b-a']

>>> [m.group() for m in regex.finditer(r"(?<aorb>[ab])-(?&aorb)", 
"XXa-bXa-aXb-bXb-aXb-cXXXX")]
['a-b', 'a-a', 'b-b', 'b-a']

As far as I can see, this is not a recursive pattern, as the parts are 
independent and not contained in each other, only one expression is tried at 
both places of the pattern.
Is it the case and is it expected/guaranteed to work this way or am I 
misunderstanding something?

 As for the other features of PHP - PCRE mentioned in
http://www.asiteaboutnothing.net/regexp/regex-tools.html#rb
(as not supported by Regex Buddy),
is it true, that "Resetting Reported Match: \K" is useless for regex, which 
supports unlimited lookarounds?

The pattern definitions (DEFINE)... also don't look that useful, as the 
repeated named expressions work very similarly.

Thanks and regards
    vbr

Original issue reported on code.google.com by Vlastimil.Brom@gmail.com on 7 Jul 2012 at 10:48

GoogleCodeExporter commented 9 years ago
A recursive expression is really just a special case of a repeated expression 
where the expression repeats itself either directly or indirectly. Although 
issue 27 is titled "Recursive patterns", it's really more general than that: 
it's repeated expressions.

As for \K, the webpage says: """The key difference between \K and a lookbehind 
is that a lookbehind does not allow you to use quantifiers: the length of what 
you look for must be fixed.""". The regex module supports variable-length 
lookbehinds, so \K, which is really just a workaround, isn't needed.

Original comment by re...@mrabarnett.plus.com on 7 Jul 2012 at 11:52

GoogleCodeExporter commented 9 years ago
> The pattern definitions (DEFINE)... also don't look that useful, as the 
repeated named expressions work very similarly.

If you are looking for the examples of (?(DEFINE)) then the following url might 
be useful for you.

http://perl5.git.perl.org/perl.git/blob/HEAD:/t/re/reg_email.t

Original comment by msm...@gmail.com on 8 Jul 2012 at 4:40

GoogleCodeExporter commented 9 years ago
Thanks for the confirmation; it's really a useful feature, which I certainly 
can use more often than the real recursive patterns.
(Maybe a remark about this functionality could be added to the documentation, 
but it isn't a big deal.)

Well, but DEFINE isn't supported in regex, is it?
I can see the complex possibilities it provides, but I don't think, I would 
(currently) want to use this from within a regex pattern in python.

However, if it would be implemented, it would certainly add further "power" in 
pattern building, if you find it reasonable.

regards,
 vbr

Original comment by Vlastimil.Brom@gmail.com on 8 Jul 2012 at 10:05

GoogleCodeExporter commented 9 years ago
Just to confirm, DEFINE isn't supported in a regex. No-one has requested it.

Original comment by re...@mrabarnett.plus.com on 8 Jul 2012 at 6:35