+ 1

Python - re library

i taken a word and need to search this literal in a text, like that word = "some" text = "this is some text" i use search method: regex = r"/"+word+"/" if re.search(regex, text) : tararara wich returns None Thanks.

23rd Mar 2021, 1:49 PM
Ivan Zanoth
Ivan Zanoth - avatar
2 Respuestas
0
regex = "some" will work fine. You don't need to surround the word with "/"s in Python. But this will also match "some" in the word "something" too. If you need "some" to appear as a standalone word, you can require it to be surrounded with whitespace. regex = "\s" + word + "\s" will do this.
23rd Mar 2021, 5:07 PM
Russ
Russ - avatar
0
Ok
23rd Mar 2021, 7:06 PM
Ivan Zanoth
Ivan Zanoth - avatar