Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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>


Comment on Re: split on commas
Select or Download Code
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?

    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), $/;
        Thanks!
      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!
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);

    produces

    $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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2015-07-04 23:35 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 (60 votes), past polls