正则表达式:删除引号分隔字符串之间的所有逗号 [python] [英] Regex : Remove all commas between a quote separated string [python]

查看:46
本文介绍了正则表达式:删除引号分隔字符串之间的所有逗号 [python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除字符串中所有逗号的合适正则表达式是什么:

12, 1425073747, "test", "1, 2, 3, ... "

结果:

12, 1425073747, "test", "1 2 3 ... "

我所拥有的正确匹配:

"((\d+), )+\d+"

但是,我显然不能用 1 美元 2 美元代替它.我不能使用 "\d+, \d+" 因为它会匹配 12, 1425073747 这不是我想要的.如果有人可以解释如何递归解析出值,那也将不胜感激.

解决方案

这应该适合你:

<预><代码>>>>input = '12, 1425073747, "test", "1, 2, 3, ... "';>>>打印 re.sub(r'(?!(([^"]*"){2})*[^"]*$),', "", input);12、1425073747、测试"、1 2 3 ..."

(?!(([^"]*"){2})*[^"]*$) 仅当在 quotea 内时才匹配文本——避免在逗号后匹配偶数个引号.

What would be an appropriate regex to remove all commas in a string as such:

12, 1425073747, "test", "1, 2, 3, ... "

Result:

12, 1425073747, "test", "1 2 3 ... "

What I have that matches correctly:

"((\d+), )+\d+"

However, I obviously cant replace this with $1 $2. I can't use "\d+, \d+" because it will match 12, 1425073747 which is not what I want. If someone can explain how to recursively parse out values that would be appreciated as well.

解决方案

This should work for you:

>>> input = '12, 1425073747, "test", "1, 2, 3, ... "';
>>> print re.sub(r'(?!(([^"]*"){2})*[^"]*$),', "", input);
12, 1425073747, "test", "1 2 3 ... "

(?!(([^"]*"){2})*[^"]*$) matches text only if inside quotea -- avoid matching even number of quotes after comma.

这篇关于正则表达式:删除引号分隔字符串之间的所有逗号 [python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆