4

我有一个字符串,

line = '12/08/2013,3,"9,25",42:51,"3,08","12,9","13,9",159,170,"3,19",437,'

我想用“:”查找并替换引号之间的逗号。寻找结果

line = '12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,'

到目前为止,我已经能够将这种模式与,

import re
re.findall('(\"\d),(.+?\")', line)

但是,我想我应该使用

re.compile(...something..., line)
re.sub(':', line)

有谁知道如何做到这一点?谢谢,实验室迷

4

3 回答 3

8
>>> import re
>>> line = '12/08/2013,3,"9,25",42:51,"3,08","12,9","13,9",159,170,"3,19",437,'
>>> re.sub(r'"(\d+),(\d+)"', r'\1:\2', line)
'12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,'

\1,\2指匹配组。


非正则表达式解决方案:

>>> ''.join(x if i % 2 == 0 else x.replace(',', ':')
            for i, x in enumerate(line.split('"')))
'12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,'
于 2013-08-26T08:29:47.240 回答
0
import re
line = '12/08/2013,3,"9,25",42:51,"3,08","12,9","13,9",159,170,"3,19",437,'
r = ""
for t in re.split(r'("[^"]*")', line):
    if t[0] == '"': 
        t = t.replace(",", ":")[1:-1]
    r += t
print r

印刷:

12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,
于 2013-08-26T08:38:40.930 回答
0

还有一个通用的正则表达式解决方案来替换双(或单)引号之间的任何类型的固定(和非固定)模式:将双引号或单引号子字符串与相应的模式匹配,并使用可调用作为替换参数到re.sub您可以操纵匹配的位置:

  1. 用冒号替换双引号之间的逗号并删除双引号(当前的OP场景):

    re.sub(r'"([^"]*)"', lambda x: x.group(1).replace(',', ':'), line)演示
    # => 12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,

  2. 用冒号替换双引号之间的逗号并保留双引号:

    re.sub(r'"[^"]*"', lambda x: x.group(0).replace(',', ':'), line)演示
    # => 12/08/2013,3,"9:25",42:51,"3:08","12:9","13:9",159,170,"3:19",437,

  3. 用冒号替换双引号和单引号之间的逗号并保留单/双引号:

    re.sub(r''''[^']*'|"[^"]*"''', lambda x: x.group(0).replace(',', ':'), '''0,1,"2,3",'4,5',''')演示
    # => 0,1,"2:3",'4:5',

此外,如果您需要处理转义的单引号和双引号,请考虑使用r"'[^\\']*(?:\\.[^\\']*)*'"(对于单引号子字符串)、r'"[^\\"]*(?:\\.[^\\"]*)*"'(对于双引号子字符串)或两者都使用 -r''''[^\\']*(?:\\.[^\\']*)*'|"[^\\"]*(?:\\.[^\\"]*)*"'''而不是上面的模式。

于 2020-05-31T12:05:48.633 回答