首页 > Python 正则split lookbehind 问题

Python 正则split lookbehind 问题

我要对一个字符串做split
代码如下

import re
x = re.split(r"(?<=,)","a,b,c,")
print x

输出的结果是:

['a,b,c,']

而我希望的结果是:

['a,','b,','c,']

等效的perl写法:

use Data::Dumper;
@x = split(/(?<=,)/,"a,b,c,");
print Dumper \@x

结果:

$VAR1 = [
      'a,',
      'b,',
      'c,'
    ];

这样不好么?

>>> re.findall(r"[^,]+,","a,b,c,")
['a,', 'b,', 'c,']

# Note that split will never split a string on an empty pattern match. For example:

>>> re.split('x*', 'foo')
['foo']
>>> re.split("(?m)^$", "foo\n\nbar\n")
['foo\n\nbar\n']

From: Python 2.7.5 documentation "7.2. re — Regular expression operations"

【热门文章】
【热门文章】