in reply to A question on splitting

split always uses a regex (except for the special case of a single blank). So you want: split /,(?! )/, $str

Replies are listed 'Best First'.
Re: Re: A question on splitting
by TASdvlper (Monk) on Jan 14, 2004 at 21:10 UTC
    So, let me see if I can understand your regexg.

    Split on "," if (0 or 1 occurances) (?) and if there is NOT (!) a space after ( ).

    So, if I wanted to check for 1 or more spaces, would this be correct ?

    split /,(?! +)/, $str
      There's no need to check for more than one space. If a comma is followed by say 5 spaces, it's also followed by a space. There's only one character relevant here, and that's the character right after the comma.

      Abigail

      Not quite. (?!pattern) is a special assertion that says the regex should only succeed if pattern would not match at that point (see perlre). So the regex has two parts: , (comma) and (?! ) (not followed by a space).

      Note that (?! ) (or it's positive counterpart (?= )) do not consume any part of the string. So /a(?=0)\d{2}/ applied to "a012" will match just "a01".

      Changing it to have a + doesn't affect anything, since if it has more than one space after the comma, it definitely has one space.