ignore special characters in python regex

A

Astan Chee

Hi,
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()
fp.close()
strin = "this is what I want"
for note in notes:
if re.search(r""+ str(note) + "",strin):
print "Found " + str(note) + " in " + strin

Thanks for any help
 
F

Frank Buss

Astan said:
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?

Maybe re.escape helps?
 
J

John Machin

Hi,
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?

It would help if you were to say

(1) what "ignore ... characters" means -- pretend they don't exist?
(2) what are "special chararacters" -- non-alphanumeric?
(3) what "treat them like normal strings" means
(4) how you expect these special characters to be (a) ignored and (b)
"treated like normal strings" /at the same time/.

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top