I want to split a string using regular expressions but I have run into some problem. I have this string:
$text=" one two three";
Then I try to split it into alphabetic words:
#@words=split(" ", $text); #1 this works
@words=split("[^a-zA-Z]", $text); #2 this doesn't work
for $word (@words){
printf "word: |$word|\n";
}
So the commented method(1) works fine. As expected I get printed:
word: |one|
word: |two|
word: |three|
However with the second method(2) I get this:
word: ||
word: |one|
word: |two|
word: |three|
So although logically the second method should be equivalent to the first one, in practice it doesn’t behave the same way. Why is that?
This is a special case in Perl’s
split()function.As stated in perldoc: