Python正则表达式在连字符和加号前面得到字符串

时间:2014-03-27 21:32:40

标签: regex python-2.7

我有一个字符串,如代码所示。我希望得到最终结果:[' AA',' BB',' CC']。 但我在这里得到的是[' AA',' BB']。你能给我一些建议吗?谢谢。

s = "AA-ZZ, BB+ZZ, CC"
a =  re.findall(r'(\w+)[-|\\+\\]\w',s)

1 个答案:

答案 0 :(得分:0)

使用预测来查看字符串是在+-还是end of string之前。

a =  re.findall(r'(\w+)(?=[-+]|$)',s)