Python regexp删除双引号内的所有逗号

时间:2019-04-30 14:12:00

标签: python regex

在某些情况下,我需要删除双引号内的所有逗号。 我有一些正在工作的东西,但看起来有些过头了,您能告诉我是否有更简单的方法可以实现? 谢谢

#!/usr/bin/env python
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"\"([^\"]*)(?=,)([^\"]*)\""

test_str = """
regular line, no double quotes here, as you can see
"wefrwerwe, werwerwer, wer,rrrr"
this is a line "where there, are doble qotes" and the commas should "be gone"
another line that has nothing special
"""

matches = re.finditer(regex, test_str, re.MULTILINE)
newtext = test_str
for match in matches:
    newtext = newtext.replace(match.group(), match.group().replace(",",""))    
print(newtext)

1 个答案:

答案 0 :(得分:0)

regex = re.compile('(".*?),(.*?")', re.I & re.M)

while regex.search(test_str):
    test_str = regex.sub('\g<1>\g<2>', test_str)