Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Help modifying recursive regex

by choroba (Chancellor)
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();
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032533]
[1nickt]: settle down beech
[Your Mother]: Catchy

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-03-28 02:50 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (326 votes). Check out past polls.