12

Have you used the Perl 5.10 backtracking control verbs in your regexes yet? And what problems did they help you accomplish?


Just as background: I have done some fiddling, but I can't get any really useful results.

As a comparison, when I started getting what the (?> grouping did, it started showing up more in my regexes. I liked the idea of tuning the world of Perl regex to a particular set of assumptions. As RE languages get more featured, the backtracking has made the performance of REs wildly divergent from the lean and mean FSA that they were based upon.


When someone can tell me what other implementation of REs has backtracking control verbs--and PCRE does not, I can concede that it belongs in the general area of expertise of people knowledgeable about regexes. This is a Perl regex question, and guys helping people out with Ruby, Python, C#, Javascript--or any PCRE client implementations-- probably can't help or see it as a waste of space for the tags they normally read.

Unihedron
  • 10,251
  • 13
  • 53
  • 66
Axeman
  • 29,194
  • 2
  • 42
  • 98
  • Did we need to create a tag for [perl-regex]? – Brad Gilbert Nov 28 '08 at 15:06
  • 1
    There are too many implementation-specific posts under the regex tag. Some people say "Only Java" or "In C#,...." So in annoys me there, as far as I know only Perl has these verbs, so it is specific to Perl. – Axeman Nov 29 '08 at 09:01
  • I don't see how "answers to the question will tend to be almost entirely based on opinions" is even a credible critique. A presented application either uses regex verbs or it does not, either works or does not, in a way similar to "solutions" presented to other questions. And whether or not it solves a *significant* enough problem, is also subject to the same type of subjectivity that the vote system itself encapsulates in the deciding whether how well a "solution" fits the question. – Axeman Oct 13 '14 at 19:18
  • Continued: Facts: Are there problems that Regex verbs can solve? reference: Yes, and I've solved them this way (which would include a regex with a control verb) specific expertise: that in 6 years since posting this question, *somebody* would have used their knowledge of regex verb to form more than "an amusing solution" and a "practical one". – Axeman Oct 13 '14 at 19:21

3 Answers3

3

Yes, I have, although not too much. I use them to control backtracking, usually to force it to make more permutations. Here’s an amusing solution and here’s a practical one.

Community
  • 1
  • 1
tchrist
  • 74,913
  • 28
  • 118
  • 169
  • 1
    At long last, I'm learning how to use some regex verbs. Thanks to you and also thanks to what appears to be an expanded section in (`perlre`)[http://perldoc.perl.org/perlre.html) (but perhaps that's you too), I actually came up with this alternative RE for the second solution: `qr{ x (*PRUNE) (a?b?c?) (?(?{$1})|(*FAIL)) }x`. – Axeman Nov 15 '12 at 16:22
2

It's been years since I did any Perl programming, so I didn't even know about this feature until you mentioned it. It looks like one of those hardcore feature that only regex gurus would use (of course, the Perl community has plenty of those). Perl 6 Grammars, on the other hand, look like they'll be a lot fun to play with.

For now, I'm content with atomic groups and possessive quantifiers.--in fact, I'm practically addicted to them. It's gotten to the point where I reflexively try to write regexes in such a way that they'll never backtrack. I have to remind myself sometimes that backtracking is okay in small doses, and it's not worth the effort to eliminate it completely.

p.s., As far as I know, possessive quantifiers are only supported by Java, PCRE (PHP, Apache, Flex 3/ActionScript 3), and the JGSoft regex engine used by RegexBuddy, EditPad Pro and PowerGrep.

update: The Oniguruma flavor (used in Ruby 1.9+ and TextMate) supports both atomic groups and possessive quantifiers. And of course, Perl 5.10 supports them in addition to the backtracking-control verbs.

Alan Moore
  • 68,531
  • 11
  • 88
  • 149
1

Honestly, I haven't even used 5.10 much yet. As great as some of the new features are I only use them in personal scripts. For production I target compatibility with 5.8. For CPAN I target 5.6. This has stopped me from playing with most of the new toys.

The backtracking control features are interesting but I can't see many applications outside of parsers. I can't imagine stuffing an entire parser into a single regex. (Even if they do support recursion now!) I'm much more excited about Perl6 grammars.

Michael Carman
  • 29,981
  • 9
  • 71
  • 121
  • I agree on grammars. But the verbs give us a little bit of that now. – Axeman Oct 31 '08 at 18:02
  • 1
    They do, but it seems more like a teaser than like anything immediately useful. There's also that little "WARNING: These patterns are experimental and subject to change or removal..." caveat in perlre. – Michael Carman Oct 31 '08 at 18:12
  • 5.10.0 is the minimum version that I program to these days. It’s just too hard to do reasonable things earlier than that. I need it for the better regex support and better Unicode support. – tchrist Nov 18 '10 at 15:28