ignore special characters in python regex
gagsl-py2 at yahoo.com.ar
Tue Jul 21 11:56:33 CEST 2009
En Tue, 21 Jul 2009 02:02:57 -0300, Astan Chee <astan.chee at al.com.au>
> I'm reading text from a file (per line) and I want to do a regex using
> these lines but I want the regex to ignore any special characters and
> treat them like normal strings.
> Is there a regex function that can do this?
> Here is what I have so far:
> fp = open('file.txt','r')
> notes = fp.readlines()
> strin = "this is what I want"
> for note in notes:
> if re.search(r""+ str(note) + "",strin):
> print "Found " + str(note) + " in " + strin
You don't even need a regex for that.
py> "fragil" in "supercalifragilisticexpialidocious"
Note that: r""+ str(note) + ""
is the same as: str(note)
which in turn is the same as: note
Remember that each line keeps its '\n' final!
for note in notes:
if note.rstrip('\n') in strin:
print "Found %s in %s" % (note, strin)
More information about the Python-list