in reply to splitting

I am not sure I understand the regex being used by perlmonkey and snowcrash's answers...

Assuming your delimiters are

' ',',', or ';',
for example, doing

perl -e'print join "!!",split /([ ,;])/,"This sentence contains, in he +re, a comma; and there's a semicolon clause\n";'
gives us:
This!! !!sentence!! !!contains!!,!!!! !!in!! !!here!!,!!!! !!a!! !!comma!!;!!!! !!and!! !!there's!! !!a!! !!semicolon!! !!clause
This trick uses the fact that
join "{some delimiter string}",@array
returns the contents of @array, separated by the delimiter string.

As you can see, things like ", " confuse it, so you might want to use a + inside the parentheses if you can afford to collect multiple separators into one array slot.

perl -e'print join "!!",split /([ ,;]+)/,"This sentence contains, in h +ere, a comma; and there's a semicolon clause\n";'
That's a bit better; now we get:
This!! !!sentence!! !!contains!!, !!in!! !!here!!, !!a!! !!comma!!; !!and!! !!there's!! !!a!! !!semicolon!! !!clause
Hope this helps...

Replies are listed 'Best First'.
RE: Re: splitting
by chromatic (Archbishop) on Apr 28, 2000 at 01:25 UTC
    The only tricky thing about the regexes supplied by the other two (besides the use of parenthesis, which is new to me and very cool) is that they specify that there is at least one whitespace character immediately before and after the delimiter. Using an asterisk would make that optional.

    Does that explain it?

      The only reason I put the \s in the regex above was because the original posting wanted: (text,delimeter,text,delimeter,text) with the white space removed ... so my regex does not capture the white space. Perhaps I was not that clear: do what ever you want with the regex, but note that whatever is in the '()' will be returned into the array. So I just as well could have done:@array = split(/(delimeter)/, $str); But then @array would have the white space in some of the array elements:
      @array would be ("text ", "delimeter', " text ", "delimeter", " text") +;
      Hopefully this will clear some things up.