0

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."

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? I tried testing my regex pattern in regex 101 and my pattern worked there. I don't know why it does not work in python. Any help will be appreciated.

Rakesh
  • 81,458
  • 17
  • 76
  • 113

1 Answers1

0

Use raw string r before the regex pattern

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")
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Hi Rakesh. Thanks for editing my code in the question and thank you for your answer. Why do I need raw string r before the pattern? What does raw string, r signify? – Ashish Ghimire Jul 10 '18 at 15:40
  • This should probably help https://stackoverflow.com/questions/12871066/what-exactly-is-a-raw-string-regex-and-how-can-you-use-it – Rakesh Jul 10 '18 at 15:42