Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: Perl Parsing Power

by protist (Monk)
on Sep 24, 2012 at 10:21 UTC ( #995343=note: print w/replies, xml ) Need Help??

in reply to Re: Perl Parsing Power
in thread Perl Parsing Power

Admittedly, it is a basic example, although powerful in its implications.
print "good string\n" if $_~~ /^({(?:[^{}]++|(?1))*+})/;
It tests for proper nesting of brackets, anchored to the beginning of the line. You can try it out:
perl -ne 'print "good string\n" if $_~~ /^({(?:[^{}]++|(?1))*+})/;'

Replies are listed 'Best First'.
Re^3: Perl Parsing Power
by BrowserUk (Pope) on Sep 24, 2012 at 10:58 UTC

    You can do that like this:

    C:\test>perl -nE"my$t; /{/&&++$t or /}/&&--$t for split''; say $t ? $t + : 'good string'" {} good string {}} -1 {{}} good string {{}{}} good string {{}{}}} -1 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}} 11 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}} 14 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +}}}} 6 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +}}}}}}}}}} good string

    But I wouldn't call it RDP :)

    The problem with doing RDP with regex is that you have to match the entire stream with one (possibly (hopefully :) composite) regex. Which is fine for yes/no booleans, but it makes both debugging the regex and reporting where and why parsing errors occur extremely messy if not impossible.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Unless I am mistaken, your oneliner will fail if given


      as input. It will report a false positive. It is also not RDP, although you know this ;).

      While I will not claim that regex RDP is pretty (complex parsing rarely is), it is possible, and Programming Perl discusses it in some minimal detail.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995343]
[choroba]: Yeah, my wife sent me some pictures of the children yesterday, and there was snow all around. 10 days in Saigon and I totally forgot what weather they have back there.
[stevieb]: We've had so much snow here this year that the whole area is pretty much under constant avalanche warnings (and avalanches pretty much daily all over the place)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-02-23 17:21 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (350 votes). Check out past polls.