2016-10-16 46 views
0

我想在Python中使用正则表达式模式分割字符串,但它不能正常工作。试图与正则表达式分割字符串

实施例文本:

"The quick {brown fox} jumped over the {lazy} dog"

代码:

"The quick {brown fox} jumped over the {lazy} dog".split(r'({.*?}))

我使用捕获组使得分割分隔符被保留在数组中。

期望的结果:

['The quick', '{brown fox}', 'jumped over the', '{lazy}', 'dog']

实际结果:

['The quick {brown fox} jumped over the {lazy} dog']

正如你可以看到有明显不匹配,因为它没有拆分字符串。任何人都可以让我知道我要去哪里吗?谢谢。

回答

1

你调用字符串分裂法,不重的

>>> re.split(r'({.*?})', "The quick {brown fox} jumped over the {lazy} dog") 
['The quick ', '{brown fox}', ' jumped over the ', '{lazy}', ' dog']