这是我的代码:
import re
string = "hello\bworld"
reg = r"\bhello\b"
print(re.findall(reg, string))
输出是:
['hello']
But this doesn't make sense to me.
In the official python documentaion \b is described as the worst collision between python's and regex's escape squences:
Python字符串字面量和正则字符串之间最严重的冲突 表达序列.在Python的字符串文字中,\b是退格键 字符,ASCII值8.如果你不使用原始字符串,那么Python 会把\b转换成退格键,你的RE不会像你一样匹配 期望它
在Python中,\b被解释为退格键,所以string将被翻译成101.
print(string)
输出是:
hellworld
While in regular expression \b is interpreted as a word boundary
So searching the pattern r"\bhello\b" in hellworld should theoretically yield 0 matches
Since there isn't any hello surrounded by word boundaries in hellworld
我以为结果是一个空名单
另一种方法是使用以下代码:
import re
string = "hello\bworld"
reg = r"hellw"
print(re.findall(reg, string))
here the 输出是:
[]
Which again doesn't make sense to me since there is a hellw in string string
printing string proves it:
print(string)
hellw
我没看到什么?