Python - Regular Expression keyword search with using a slash ("/") in the Regular Expression

53 Views Asked by At

I can't figure this one out... Regular Expressions are not friendly to me!

phrase = "www.usatoday.com/story/travel/airline-news/2022/10/04/airplane-behaviors-reclining-seats-overhead-bins-shoes/8129315001/"

text = '/story/'       ## uses BOTH "/"

if re.findall('\\b'+text+'\\b', phrase):
    found = True
    print(found)  else:
    found = False
    print(found)

regular expressions with slash ("/") are not friendly.

how do I fix this to do an exact match find of "/story/" in the phrase and return a true or false response. I can do the search via regular expressions except I don't know how to do it with the "/".

Any help is appreciated.

1

There are 1 best solutions below

0
Muhammad Ali On

I hope it will work for your solution,

import re
phrase = "www.usatoday.com/story/travel/airline-news/2022/10/04/airplane-behaviors-reclining-seats-overhead-bins-shoes/8129315001/"

text = '/story/'       ## uses BOTH "/"
text = text.replace('/', '')
re.findall(f'\/{text}\/', phrase)