Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Help modifying recursive regex

by choroba (Canon)
on May 07, 2013 at 20:06 UTC ( #1032533=note: print w/ replies, xml ) Need Help??


in reply to Help modifying recursive regex

Not using a regex, but a simple automaton:

#!/usr/bin/perl use warnings; use strict; use Test::More; sub groups { my $string = shift; my $depth = 0; my @return = (q()); for my $char (split //, $string) { $depth++ if '(' eq $char; $depth-- if ')' eq $char; if ((2 > $depth and qw/) (/[$depth] eq $char) or (' ' eq $char and ! $depth)) { push @return, q(); } else { $return[-1] .= $char; } } s/^ *| *$//g for @return; # trim return [ grep length, @return]; } is_deeply(groups("A (B C D)"), ["A", "B C D"]); is_deeply(groups("A (B C D) (E F G)"), ["A", "B C D", "E F G"]); is_deeply(groups("A B (C (D E) (F G)) (H I)"), ["A", "B", "C (D E) (F G)", "H I"]); is_deeply(groups("A B (C (D E) (F G)) X Y (H I)"), ["A", "B", "C (D E) (F G)", "X", "Y", "H I"]); done_testing();
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Help modifying recursive regex
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-31 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls