AlanOlsen has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to split on a comma, but only where it is not in parenthisis.

For example:

var-a numeric (10,0) = NULL, var-b char (10) = NULL

would split to:

var-a numeric (10,0) = NULL var-b char (10) = NULL

instead of:

var-a numeric (10 0) = NULL var-b char (10) = NULL

Ideas? I should know this, but i have not had enough coffee today.

Replies are listed 'Best First'.
Re: Regex question
by TedYoung (Deacon) on Oct 20, 2005 at 17:48 UTC

    Well, if you don't have to worry about nested parens, this seems to work for me:

    /,(?![^()]*\))/

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      Works great! Thanks!
Re: Regex question
by ww (Archbishop) on Oct 20, 2005 at 18:01 UTC
    Here's one ugly way:

    UPDATE: UGLIER than I realized; see Roy Johnson's below

    use strict; use warnings; use vars qw ($foo $bar @bar); $foo = 'var-a numeric (10,0) = NULL, var-b char (10) = NULL'; @bar = split /([^(,)].*,)+/, $foo; # [^(,)]... happens to work here, but is NOT a generally appli +cable approach! foreach $bar(@bar) { print "$bar\n"; }
    OUTPUT:
    var-a numeric (10,0) = NULL,
     var-b char (10) = NULL
    
      Not only ugly, it doesn't match the OP's spec. It matches everything from the first non-special character through the last comma. Try it with
      $foo = 'var-a numeric (10,0) = NULL, var-b char (10) = NULL, var-c dum +my (1, 5)';
      Since you're specifying a capture group, you're probably better trying for a m//g style capture, rather than using split. Like so:
      my $str = 'var-a numeric (10,0) = NULL, var-b char (10) = NULL, var-c +dummy (1, 5)'; my @bits = $str =~ /(?:^|,) # Match starting at beginning of string or a comma ( # Capture (?: # group of [^(,] # non-parens | # or \(.*?\) # open-paren to close-paren )* # as many times as possible )/gx; print ">$_<\n" for @bits;

      Caution: Contents may have been coded under pressure.
Re: Regex question
by Nkuvu (Priest) on Oct 20, 2005 at 17:56 UTC
    I'd suggest a module like Text::xSV, but I am not sure if it will work with parens. I do know that it works well with quoted comma-separated values, though.