If you need to check if one line starts with another (this is what re.match does, since it only matches at the beginning of the line), use each.startswith(temp) . Or use re.match(re.escape(temp), each) , since re.escape will automatically exit (add \ ) in front of all the "special" characters.
If you need to check whether one begins with the other, you do not need a regular expression:
each.startswith(temp)
or with regex:
m = re.match(re.escape(temp), each)
If you need to check if it is equal to another, you need to add an end of line binding:
m = re.match("{0}$".format(re.escape(temp)), each)
If you need to check if one of them contains, you need to use re.search :
m = re.search(re.escape(temp), each)
or not a regular expression:
each.find(temp) > -1
Watch the IDEONE demo :
import re temp="PID ie() Invoked u 5 1n 5n T ss" each="PID ie() Invoked u 5 1n 5n T ss" # Check if one starts with another print(each.startswith(temp)) # Same with regex: print(re.match(re.escape(temp), each)) # Check if one *equals* another print(re.match("{0}$".format(re.escape(temp)), each)) # Check if one *contains* another print(each.find(temp) > -1) print(re.search(re.escape(temp), each))
source share