Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Text::Balanced with nested / custom brackets

by ikegami (Pope)
on Sep 07, 2006 at 21:48 UTC ( #571810=note: print w/ replies, xml ) Need Help??


in reply to Text::Balanced with nested / custom brackets

As far as I can tell, Text::Balanced deals with single-character delimiters, whereas your delimiter has two. You might have to resort to using a regexp.

my $extractor; # Must be a seperate statement. $extractor = qr/ \[\[ (?: (?: (?! \[\[ | \]\] ) . )+ | (??{ $extractor }) )+ \]\] /x; my @links = $text =~ /$extractor/g;

Optimized (I think):

my $extractor; # Must be a seperate statement. $extractor = qr/ \[\[ (?> (?: (?: (?> [^\[\]]+ ) | \[ (?! \[ ) | \] (?! \] ) ) | (??{ $extractor }) )+ ) \]\] /x; my @links = $text =~ /$extractor/g;

Tested.


Comment on Re: Text::Balanced with nested / custom brackets
Select or Download Code
Re^2: Text::Balanced with nested / custom brackets
by Anonymous Monk on Sep 07, 2006 at 22:18 UTC
    Thank you for those regexes! I'll play around with them to see if I can get myself moving.

    In the long run, I'd still like to know if Text::Balanced can be massaged into dealing with this situation. It does deal with <tags> and such..

      The function to extract tagged data can indeed be used.

      my @links; my $extractor = gen_extract_tagged('[[', ']]', qr/(?:(?!\[\[).)*/); for (;;) { (my $link, $text) = $extractor->($text); last if not defined $link; push(@links, $link); }

      Untested.

Re^2: Text::Balanced with nested / custom brackets
by wojtyk (Friar) on Sep 08, 2006 at 15:56 UTC
    It's worth noting that ikegami's code is essentially a derivation of code in perlre for matching balanced parens:
    $re = qr{ \( (?: (?> [^()]+ ) # Non-parens without backtracking | (??{ $re }) # Group with matching parens )* \) }x;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://571810]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-12-25 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls