in reply to Regex question

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

Replies are listed 'Best First'.
Re^2: Regex question
by Roy Johnson (Monsignor) on Oct 20, 2005 at 18:06 UTC
    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.