Regex 在字符串中查找特定单词的正则表达式模式

Regex 在字符串中查找特定单词的正则表达式模式,regex,python-3.x,Regex,Python 3.x,我想要一个简单的正则表达式模式来查找字符串中的特定单词。例如,在下面的代码中,我想知道字符串是否有单词“wings” 上面的代码不是应该打印“匹配”吗,因为字符串body中有“wings”这个词吗?我试着在regex101中测试我的regex模式,我的模式在那里工作。我不知道为什么它在python中不起作用。任何帮助都将不胜感激。在正则表达式模式之前使用原始字符串r Ex: import re body = """ Beginning on Wednesday, April 13, contr

我想要一个简单的正则表达式模式来查找字符串中的特定单词。例如,在下面的代码中,我想知道字符串是否有单词“wings”


上面的代码不是应该打印“匹配”吗,因为字符串body中有“wings”这个词吗?我试着在regex101中测试我的regex模式,我的模式在那里工作。我不知道为什么它在python中不起作用。任何帮助都将不胜感激。

在正则表达式模式之前使用原始字符串
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")

嗨,拉凯什。谢谢你在问题中编辑我的代码,谢谢你的回答。为什么在模式之前需要原始字符串r?原始字符串r表示什么?这可能会有所帮助
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")