英文:
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'([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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论