newlist = ['test', '%ing', 'osh', '16fg']
tartext = 'Singing'
I want to check my tartext value doesn't matching with any value with newlist. if the newlist string contains % symbol in the value then I need to match this as wildcard charecter.
I want to achieve condition as below.
if (tartext != 'test' and tartext not like '%ing' and tartext != 'osh' and tartext !=
'16fg') then return true else false
Since %ing
from the lsit contains '%' symbol in it I need to change the comparison as wildcard chatecter search like sql.
In this example 'Singing' is matching with '%ing' then I am expecting the condition to return False.
Below is the code that I have tried but didn't work
import re
newlist = ['test', '%ing', 'osh', '16fg']
tartext = 'Singing'
def wildcard_compare(string, match):
match = match.replace('%','.*')#.replace('_','.')
match_expression = f'^{match}$'
return bool(re.fullmatch(match_expression,string))
def condition_match(lookupstring, mylist):
for value in mylist:
if '%' in value:
if not wildcard_compare(lookupstring,value):
return True
else:
if value != lookupstring:
return True
return False
print(condition_match(tartext,newlist))
print(condition_match('BO_IN',['AP_IN','BO_IN','CA_PS']))
One approach would be to use regular expressions. In this approach, we can replace %
in the newlist
by .*
. Then, use re.search
along with a list comprehension to find any matches.
newlist = ['test', '%ing', 'osh', '16fg']
tartext = 'Singing'
newlist = [r'^' + x.replace('%', '.*') + r'$' for x in newlist]
num_matches = sum([1 for x in newlist if re.search(x, tartext)])
if num_matches > 0:
print("found a match") # prints here
else:
print("no matches")
Edit:
See the comment below by @ekhumoro in the case that your input string might also have regex metacharacters in it.