[英]re.sub “(-)” failed
I am not able to replace the string "(-)" using re.sub in Python. 我无法在Python中使用re.sub替换字符串“(-)”。
>>> 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). 另外,在字符串前面添加
r
以保持其原始(由于反斜杠)。
Or don't use regexp at all (if your substitution is that simple) and you don't have to care about many issues: 或者根本不使用regexp(如果您的替换就这么简单),并且您不必关心许多问题:
>>> 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 : 调试这种事情的一种方法是使用
re.DEBUG
标志 :
>>> 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) (我添加了
#
之后的内容,不是来自调试输出)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.