Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: split on commas

by Anonymous Monk
on Jun 05, 2009 at 18:07 UTC ( #768870=note: print w/replies, xml ) Need Help??

in reply to split on commas

Perhaps this?
$_ = "<*2>FOO<2,1>,<*3>(SigB<8:0:2>,BAR),<*2>Siga<2:0>,Sigb<8,7,6,5 +,0>"; @line = split(/,(?![\w,]+[>)])/, $_); print join("\n", @line), $/;
Which prints:
<*2>FOO<2,1> <*3>(SigB<8:0:2>,BAR) <*2>Siga<2:0> Sigb<8,7,6,5,0>

Replies are listed 'Best First'.
Re^2: split on commas
by pip9ball (Acolyte) on Jun 05, 2009 at 18:10 UTC
    Yes, this is what I need. Perhaps you can explain your split line?

      Using YAPE::Regex::Explain:
      #!/usr/bin/env perl use strict; use warnings; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(',(?![\w,]+[>)])')->explain(); __END__ The regular expression: (?-imsx:,(?![\w,]+[>)])) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- , ',' ---------------------------------------------------------------------- (?! look ahead to see if there is not: ---------------------------------------------------------------------- [\w,]+ any character of: word characters (a-z, A-Z, 0-9, _), ',' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- [>)] any character of: '>', ')' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
        Cool, I wasn't aware of this module. Thanks!
      With comments:
      $_ = "<*2>FOO<2,1>,<*3>(SigB<8:0:2>,BAR),<*2>Siga<2:0>,Sigb<8,7,6,5,0> +"; @line = split(/ , # comma... (?! # not followed by [\w,]+ # "word" characters or commas and [>)] # a close-bracket or close-paren ) /x, $_); print join($/, @line), $/;
Re^2: split on commas
by ig (Vicar) on Jun 06, 2009 at 11:22 UTC

    This does not work for some strings.

    $string = "(1,2,3,<4,5,6>),more"; my @line = split(/,(?![\w,]+[>)])/, $string); print Dumper(\@line);


    $VAR1 = [ '(1', '2', '3', '<4,5,6>)', 'more' ];

    The problem is any commented comma that is followed by an opening delimiter before a closing delimiter. I suspect it is not possible to generalize the solution without counting the delimiters.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://768870]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-04-27 02:01 GMT
Find Nodes?
    Voting Booth?