英文:
Regex: keep part of pattern multiple times
问题
我想使用正则表达式将","替换为", "(逗号后跟空格),但不要在字符串末尾:
"a,b,c,d," 替换为 "a, b, c, d,"
同时也包括
"Berlin,London,Offenbach,Gera," 替换为 "Berlin, London, Offenbach, Gera,"
或者
"123,2345,653,12," 替换为 "123, 2345, 653, 12,"
我在Python中尝试了以下代码:
import re
re.sub(r'([1-9a-zA-Z]),([1-9a-zA-Z])', r', ', "a,b,c,d,")
但是得到了结果:
"a, b,c, d,"("b"和"c"之间的逗号后没有空格)
有什么问题?
英文:
I want to use regex for replacing "," with ", "(comma followed by space) but not at the end of the string:
"a,b,c,d," to "a, b, c, d,"
but also
"Berlin,London,Offenbach,Gera," to "Berlin, London, Offenbach, Gera,"
or
"123,2345,653,12," to "123, 2345, 653, 12,"
I tried in Python
import re
re.sub(r'([1-9a-zA-Z]),([1-9a-zA-Z])', r', ', "a,b,c,d,")
but get the result:
'a, b,c, d,' (space after comma between "b" and "c" is missing)
What's wrong?
答案1
得分: 1
你可以简单地替换 ,(?=.)
,这只匹配不出现在输入末尾的逗号。
inp = "a,b,c,d,"
output = re.sub(r',(?=.)', ', ', inp)
print(output) # a, b, c, d,
英文:
You may simply replace on ,(?=.)
, which will only match commas not occurring at the end of the input.
<!-- language: python -->
inp = "a,b,c,d,"
output = re.sub(r',(?=.)', ', ', inp)
print(output) # a, b, c, d,
答案2
得分: 1
你可以使用 ,(?!$)
来匹配除了行尾的逗号之外的任何逗号。
import re
print(re.sub(r',(?!\s*$)', ', ', "a,b,c,d,")+'|')
a, b, c, d,|
(|
用来显示缺少尾随空格)
解释:
(?! ... )
是负向前瞻。它检查前一个匹配不是紧跟着此组的内容。$
- 表示字符串结束的元字符。
英文:
You can use ,(?!$)
to match any comma except in the end of line.
import re
print(re.sub(r',(?!$)', r', ', "a,b,c,d,")+'|')
a, b, c, d,|
(|
to see lack of trailing space)
Explanation:
(?! ... )
is a negative lookahead. It checks that previous match is not followed by content of this group.$
- meta symbol meaning end of string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论