Pit*_*kos 2 regex string perl split delimiter
我想使用正则表达式拆分字符串,但我遇到了一些问题.我有这个字符串:
$text=" one two three";
Run Code Online (Sandbox Code Playgroud)
然后我尝试将其拆分为字母词:
#@words=split(" ", $text); #1 this works
@words=split("[^a-zA-Z]", $text); #2 this doesn't work
for $word (@words){
printf "word: |$word|\n";
}
Run Code Online (Sandbox Code Playgroud)
所以评论方法(1)工作正常.正如所料,我得到印刷:
word: |one|
word: |two|
word: |three|
Run Code Online (Sandbox Code Playgroud)
但是使用第二种方法(2)我得到了这个:
word: ||
word: |one|
word: |two|
word: |three|
Run Code Online (Sandbox Code Playgroud)
因此,虽然逻辑上第二种方法应该等同于第一种方法,但实际上它的行为方式并不相同.这是为什么?
归档时间: |
|
查看次数: |
190 次 |
最近记录: |