Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a string if it contains a string that's inside a list python

Tags:

python

string

String = "Alienshave just discovered a way to open cans"
Arr=["Aliens","bird","cactus","John Cena"]

if any(words in String for words in arr):
       print String

This script displays Alienshave just discovered a way to open cans

but i dont want it to print String since the word Alienshave in String is not exactly the same as the word Aliens found in Arr

How do i do this so that the basis for comparison are the strings inside an array and doesnt act like a wildcard.

like image 249
Boneyflesh Avatar asked Mar 24 '26 20:03

Boneyflesh


1 Answers

Using regular expression with word boundary(\b):

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of Unicode alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore Unicode character. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.


string = "Alienshave just discovered a way to open cans"
arr = ["Aliens","bird","cactus","John Cena"]

import re
pattern = r'\b({})\b'.format('|'.join(arr)) # => \b(Aliens|bird|cactus|John Cena)\b
if re.search(pattern, string):
    print(string)
# For the given `string`, above `re.search(..)` returns `None` -> no print
like image 183
falsetru Avatar answered Mar 26 '26 09:03

falsetru