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

Is it possible to use a regular expression (or something like a regular expression) in a split directly. I've already figured that I could do a substitution prior to the split. I was just curious about the limits of the split function.

Replies are listed 'Best First'.
Re: A Split Question
by btrott (Parson) on May 18, 2000 at 21:53 UTC
    Yes, you can definitely use a regular expression in split. In fact, that's what split expects you to give it.

    For example, I could say

    my $string = "foo , bar"; my @pieces = split /\s*,\s*/, $string;
    to split $string on a comma surrounded by optional whitespace.

    A regular expression is different than a substitution, though. So I'm not sure exactly what you're trying to do. Could you give a little more context for the particular problem you're working on?

      It's btrott who responded so I know he knows, but does the questioner know about the greedy default behaviour of * and +, and the dangers when used in conjunction with .
      Its just that I spent a long time figuring that one out, and would'nt wish that headache on Xxaxx or anyone else. So, check out p29 of your Camel, unless you know all about regexes, and I'm wasting both your time and mine. :-)
Re: A Split Question
by princepawn (Parson) on May 18, 2000 at 21:54 UTC
    Well, under unix: man perlfunc or perldoc perlfunc does indeed state that the first argument to split is a regular expression...