[英]Regex pattern to find a specific word in a string
I want a simple regex pattern to find a specific word in a string. 我想要一个简单的正则表达式模式来找到字符串中的特定单词。 For instance, in the code below, I want to find if a string has the word, "wings."
例如,在下面的代码中,我想查找一个字符串是否包含单词“ wings”。
import re
body = """
Beginning on Wednesday, April 13, contractors will commence renovation of the
floors, staircase, ceilings and restrooms in Core 5 of the Phase I Academic
Building (the area between wings D and E). During construction, access to Core..
"""
if re.search('\bwings\b', body, re.IGNORECASE):
print("Matches")
else:
print("Does not match")
Isn't the code above supposed to print "Matches" because the word, "wings" exists in the string, body? 上面的代码难道不应该打印“匹配”,因为字符串“ body”中存在“ wings”一词吗? I tried testing my regex pattern in regex 101 and my pattern worked there.
我尝试在regex 101中测试我的regex模式,并且我的模式在那里工作。 I don't know why it does not work in python.
我不知道为什么它在python中不起作用。 Any help will be appreciated.
任何帮助将不胜感激。
Use raw string r
before the regex pattern 在正则表达式模式之前使用原始字符串
r
Ex: 例如:
import re
body = """
Beginning on Wednesday, April 13, contractors will commence renovation of the floors, staircase, ceilings and restrooms in Core 5 of the Phase I Academic Building (the area between wings D and E). During construction, access to Core..
"""
if re.search(r'\bwings\b', body, re.IGNORECASE):
print("Matches")
else:
print("Does not match")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.