Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Maximal match in a recursive regex

by diotalevi (Canon)
on Jun 26, 2003 at 17:06 UTC ( #269318=note: print w/ replies, xml ) Need Help??


in reply to Maximal match in a recursive regex

Ah I see. The key is to change the capturing group to match one or more times instead of just once. It just becomes )+ from ).

Added: I goofed. That is *part* of the key. The above change has a maximal match but loses the contents of the non-innermost matches. Here's a version that *works*

$re = qr/ \[ # Opening bracket ((?: # Capture the contents [^][]+ # Body of a link | (??{$re}) # Or recurse )+) # and allow repeats internally \] # Closing bracket /x;

Noted: for a brief period there was also some pushing into @f. That shouldn't have been posted so I removed it.


Comment on Re: Maximal match in a recursive regex
Select or Download Code
Re: Re: Maximal match in a recursive regex
by Mr. Muskrat (Abbot) on Jun 26, 2003 at 17:35 UTC

    This is how I fixed it.

    my $re; $re = qr/ \[ # Opening bracket ( # Capture the contents [^][]+ # Body of a link | (??{$re}) # Or recurse )+ # added per diotalevi's instructions \] # Closing bracket /x; $k = "a[b[c[d]]"; $k =~ s/($re)/<$1>/g; # I added the ()'s :) print $k;

    Bah! It captures the outer square brackets too :(

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-07-22 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (109 votes), past polls