开发者

Regular expressions in python : don't split when a specific substring is found

I found a bug in a regex in one of python-scripts and I need help to solve it. The problem is that the script splits a string with re.split('(V[A-Z])', string) but I don't want a split 开发者_Python百科if the specific substring SVD is found.

example: "ACD VU LSF VMSUGH VIJ SVD HJV DVO" -> "ACD ", "U LSF ","MSUGH ", "IJ SVD HJV D", "O"

Is there someone out there that can solve my problem?


If I understand your question correctly, you should use lookbehind assertion in regex. http://docs.python.org/library/re.html

>>>x = 'ACD VU LSF VMSUGH VIJ SVD HJV DVO'
>>>result = re.split('V[A-Z](?<!SVD)', x)
['ACD ', ' LSF ', 'SUGH ', 'J SVD HJV D', '']


Hmm...

if 'SVD' not in string:
    result = re.split('(V[A-Z])', string)
else:
    result = string
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜