替换python中与regex匹配的字符

2024-06-26 02:07:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想替换文本中出现的“,”。我不想替换所有“,”因为它是一个csv文件。为此,我编写了regex,它标识包含不需要的逗号的文本。下面是我的regex101链接
http://regex101.com/r/vF2iO5

它正确地识别了我的文字

 "_id" : "Java code PMD Complains about Cyclomatic Complexity , of 20", "tags" : "java   performance tuning pmd", "title" : "Java code PMD Complains about Cyclomatic Complexity , of 20", "results" : true, "value" : true, "processed" : true, "tokenGenerated" : [ "java", "code", "pmd", "complains" ] 

它在键“\u id”和包含逗号的“title”中标识文本。现在我只想用“@@@”这样的符号替换文本中的这两个逗号。我该怎么做??你知道吗

我的正则表达式是

\"[(\w)(\s)]+ (\,) [(\w)(\s)]+\"

编辑

用python和回复sub如下所示。但是我应该在替换部分写什么??你知道吗

re.sub(r'(\"[(\w)(\s)]+\,[(\w)(\s)]+\")',r'\0',str(text)) 

Tags: of文本idtruecodejava标识about
2条回答

您可以使用re.sub

import re

s = '''"_id" : "Java code PMD Complains about Cyclomatic Complexity , of 20", "tags" : "java   performance tuning pmd", "title" : "Java code PMD Complains about Cyclomatic Complexity , of 20", "results" : true, "value" : true, "processed" : true, "tokenGenerated" : [ "java", "code", "pmd", "complains" ]'''

>>> print re.sub(r'(\"[(\w)(\s)]+ )(,)( [(\w)(\s)]+\")', '\\1@@@\\3', s)
"_id" : "Java code PMD Complains about Cyclomatic Complexity @@@ of 20", "tags" : "java   performance tuning pmd", "title" : "Java code PMD Complains about Cyclomatic Complexity @@@ of 20", "results" : true, "value" : true, "processed" : true, "tokenGenerated" : [ "java", "code", "pmd", "complains" ]

你可以用sub

re.sub(r'(\"[(\w)(\s)]+ )(,)([(\w)(\s)]+\")', '@@@', s) '"_id" : @@@, "tags" : "java performance tuning pmd", "title" : @@@, "results" : true, "value" : true, "processed" : true, "tokenGenerated" : [ "java", "code" , "pmd", "complains" ]'

相关问题 更多 >