首页 > 解决方案 > 不明白为什么简单的正则表达式不匹配

问题描述

我期望以下模式:

pattern1=r"\Ar(evolution)?ide\bthe\b(earth)\Z"

匹配:

string=r"ride the earth"

相反,以下模式匹配:

pattern2=r"r"\Ar(evolution)?ide \bthe\b (earth)\Z"

\b空白一样,为什么模式 1 不匹配字符串?

标签: pythonregexpython-3.x

解决方案


\b不是空间;它匹配单词开头或结尾的空字符串。

\s是一个空白字符,所以会匹配。r"\Ar(evolution)?ide\sthe\s(earth)\Z"


推荐阅读