I have following:
temp = "aaaab123xyz@+"
lists = ["abc", "123.35", "xyz", "AND+"]
for list in lists
if re.match(list, temp, re.I):
print "The %s is within %s." % (list,temp)
The re.match is only match the beginning of the string, How to I match substring in between too.
Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.
Python set() method and == operator to compare two lists Further, the == operator is used for comparison of the data items of the list in an element-wise fashion.
The any() function is used to check the existence of an element in the list. it's like- if any element in the string matches the input element, print that the element is present in the list, else, print that the element is not present in the list. Example: Python3.
Lists are one of the most common data structures in Python, and they are often used to hold strings.
You can use re.search
instead of re.match
.
It also seems like you don't really need regular expressions here. Your regular expression 123.35
probably doesn't do what you expect because the dot matches anything.
If this is the case then you can do simple string containment using x in s
.
Use re.search
or just use in if l in temp:
Note: built-in type list
should not be shadowed, so for l in lists:
is better
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With