正则表达式:多次保留模式的一部分

huangapple go评论69阅读模式
英文:

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 = &quot;a,b,c,d,&quot;
output = re.sub(r&#39;,(?=.)&#39;, &#39;, &#39;, 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&#39;,(?!$)&#39;, r&#39;, &#39;, &quot;a,b,c,d,&quot;)+&#39;|&#39;)
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.

huangapple
  • 本文由 发表于 2023年4月4日 16:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定