正则表达式\b匹配单词边界,结果和我预期不同,帮看看下!

JeanLove 2018-05-06 12:39:56
print re.match("\bThe\b","in The word").group()


我期待"\b"是用来“匹配任何单词边界”

为什么这个匹配没有返回任何结果呢?
...全文
398 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
欢乐的小猪 2018-05-06
  • 打赏
  • 举报
回复
Python提供了基于正则表达式的两种不同的原始操作:re.match只在字符串的开头检查匹配,而re.search检查字符串中的任何匹配项。
qq_41619756 2018-05-06
  • 打赏
  • 举报
回复
字符串前需要加r进行转意
MaMMoN2017 2018-05-06
  • 打赏
  • 举报
回复
re.match(pattern, string, flags=0)¶ If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match. Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line. If you want to locate a match anywhere in string, use search() instead (see also search() vs. match()). match 用来比较pattern 和 string, 只会匹配字符串的起始部分, 你用错函数了.
oyljerry 2018-05-06
  • 打赏
  • 举报
回复
re.search("\\bThe\\b","in The word")

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧