简体   繁体   中英

re.sub “(-)” failed

I am not able to replace the string "(-)" using re.sub in Python.

>>> instr = 'Hello, this is my instring'
>>> re.sub('my', 'your', instr)
'Hello, this is your instring'
>>> instr = 'Hello, this is my (-) instring'
>>> re.sub('my (-)', 'your', instr)
'Hello, this is my (-) instring'

Can somebody please give me a hint what I am doing wrong.

Thank you!

re.sub(r'my \(-\)', 'your', instr)

You have to escape the parenthesis, which are normally used for matching groups. Also, add an r in front of the string to keep it raw (because of backslashes).

Or don't use regexp at all (if your substitution is that simple) and you don't have to care about many issues:

>>> instr = 'Hello, this is my (-) instring'
>>> instr.replace('my (-)', 'your')
'Hello, this is your instring'

You need to escape the '(-)' because it is a regular expression pattern match, as far as the regex engine is concerned. If you're not sure about how to escape, but your string doesn't have any actual patterns but should be interpreted verbatim, you should do:

>>> re.sub(re.escape('my (-)'), 'your', instr)
'Hello, this is your instring'

or if your string is a mix between a "plain" pattern and complex stuff, you can do this:

>>> re.sub('[a-z]{2} %s' % re.escape('(-)'), 'your', instr)
'Hello, this is your instring'

One way to debug things like this is to use the re.DEBUG flag :

>>> import re

>>> p = re.compile("my (-)", re.DEBUG)
literal 109 # chr(109) == "m"
literal 121 # chr(121) == "y"
literal 32 # chr(32) == " "
subpattern 1 # a group
  literal 45 # chr(45) == "-"
<_sre.SRE_Pattern object at 0x1004348a0>

So this matches "-" in a group, no where does it match for a literal ( , compared to:

>>> re.compile(r"my \(-\)", re.DEBUG)
literal 109
literal 121
literal 32
literal 40 # chr(40) == "(", not a group this time
literal 45
literal 41
<_sre.SRE_Pattern object at 0x10043ea48>

(the stuff after # was added by me, it's not from the debug output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM