in reply to Re: parsing question
in thread parsing question

Abigail's extended regular expression is also an opportunity to show you a nifty module called YAPE::Regex::Explain.

ladoix% cat 290992 #!/usr/bin/perl use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/_test(?>\s+)(?!<)/)->explain; ladoix% perl 290992 The regular expression: (?-imsx:_test(?>\s+)(?!<)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- _test '_test' ---------------------------------------------------------------------- (?> match (and do not backtrack afterwards): ---------------------------------------------------------------------- \s+ whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?! look ahead to see if there is not: ---------------------------------------------------------------------- < '<' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

--
Allolex

Replies are listed 'Best First'.
Re: parsing question
by Abigail-II (Bishop) on Sep 12, 2003 at 12:50 UTC
    Unfortunally, it only explains what it does, but it doesn't explain why it does so. Perhaps the most subtle part of the regex is (?>\s+).

    Can you explain why it uses "no backtracking"? ;-)

    Abigail

      I would love to know why you did what you did with that regex. There are probably a lot of monks in the Monastery who could learn something from you and just need a little push in the right direction. :)

      --
      Allolex

        #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; print "with (?>): '$_' matches\n" if /_test(?>\s+)(?!<)/; print "without (?>): '$_' matches\n" if /_test\s+(?!<)/; } __DATA__ _test foo _test < _test < _test<

        Running this gives:

        with (?>): '_test foo' matches without (?>): '_test foo' matches without (?>): '_test <' matches

        Abigail

      I'm sure you're familiar with the "cut" operator, Abigail, but most of the other people here will find it largely underdocumented. So I dare to feel free to point towards the draft of a book on regular expressions that the same perlmonk is writing, who also wrote YAPE::Regex::Explain (actually, he wrote the whole YAPE suite).

      I personally like the draft of book very much, and so, I like to plug it whenever I can. So here's the URL: http://japhy.perlmonk.org/book/. Check out chapter 8 for the "cut" operator — every chapter is a separate download, roughly around 12 pages each, either in MS Word or in PDF format. Recommended.