如何使用正则表达式将 ‘'+'’ 替换为 ‘'plus'’。

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

how to replace '+' with 'plus' using regular expression

问题

I'd like to replace [0-9]+e|E\+[0-9]+ with [0-9]+e|Eplus[0-9]+ and [0-9]+e|E-[0-9]+ with [0-9]+e|Eminus[0-9]+.

I tried this python script:

re.sub(r'[0-9]+[eE]\+[0-9]+$', r'[0-9]+[eE]plus[0-9]+', 'a1e+1b+a')

I hope to get 'a1eplus1b+a', but it doesn't work. How to do that properly?

英文:

I'd like to replace

[0-9]+e|E\+[0-9]+ with [0-9]+e|Eplus[0-9]+

and

[0-9]+e|E-[0-9]+ with [0-9]+e|Eminus[0-9]+

I tried this python script:

re.sub(r'[0-9]+[eE]\+[0-9]+$', r'[0-9]+[eE]plus[0-9]+$', 'a1e+1b+a')

I hope to get 'a1eplus1b+a', but it doesn't work. How to do that properly?

答案1

得分: 1

The second argument to re.sub is not a regex. You need to use a completely different syntax for the replacement string. In very brief, you can refer back to numbered matching groups with \1, \2, etc;

re.sub(r'([0-9]+[eE])\+([0-9]+)', r'plus', 'a1e+1b+a')

... or, you can use lookarounds to just match and replace the part you really want to replace:

re.sub(r'(?<=[0-9][eE])\+(?=[0-9])', r'plus', 'a1e+1b+a')

The second one also extends nicely to a solution where you can do minus and plus in one fell swoop:

re.sub(r'(?<=[0-9][eE])[-+](?=[0-9])',
       lambda match: {'-': 'minus', '+': 'plus'}[match.group()],
       'a1e+1b+a')

Here, the lambda expression receives the re.Match object corresponding to the match, and use its .group() method to extract the string which matched. We use that as an index into the dictionary of replacements, to obtain the spelled-out string corresponding to either 'plus' or 'minus'.

Python's re module does not allow lookarounds to be variable length, but that's not really useful here anyway, so I just took out the + after [0-9]. Notice also that I took out the $ which prevented your test case from matching.

英文:

The second argument to re.sub is not a regex. You need to use a completely different syntax for the replacement string. In very brief, you can refer back to numbered matching groups with \1, \2, etc;

re.sub(r&#39;([0-9]+[eE])\+([0-9]+)&#39;, r&#39;plus&#39;, &#39;a1e+1b+a&#39;)

... or, you can use lookarounds to just match and replace the part you really want to replace:

re.sub(r&#39;(?&lt;=[0-9][eE])\+(?=[0-9])&#39;, r&#39;plus&#39;, &#39;a1e+1b+a&#39;)

The second one also extends nicely to a solution where you can do minus and plus in one fell swoop:

re.sub(r&#39;(?&lt;=[0-9][eE])[-+](?=[0-9])&#39;,
       lambda match: {&#39;-&#39;: &#39;minus&#39;, &#39;+&#39;: &#39;plus&#39;}[match.group()],
       &#39;a1e+1b+a&#39;)

Here, the lambda expression receives the re.Match object corresponding to the match, and use its .group() method to extract the string which matched. We use that as an index into the dictionary of replacements, to obtain the spelled-out string corresponding to either &#39;plus&#39; or &#39;minus&#39;.

Python's re module does not allow lookarounds to be variable length, but that's not really useful here anyway, so I just took out the + after [0-9]. Notice also that I took out the $ which prevented your test case from matching.

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

发表评论

匿名网友

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

确定