Regex with ASCII and non-ASCII chars

T

TOXiC

Hello everybody.
How I can do a regex match in a string with ascii and non ascii chars
for example:

regex = re.compile(r"(ÿÿ‹ð…öÂty)", re.IGNORECASE)
match = regex.search("ÿÿ‹ð…öÂty")
if match:
result = match.group()
print result
else:
result = "No match found"
print result

it return "no match found" even if the two string are equal.
Help me please!
Thx in advance :)
 
P

Peter Otten

TOXiC said:
How I can do a regex match in a string with ascii and non ascii chars
for example:

regex = re.compile(r"(ÿÿ?ð?öÂty)", re.IGNORECASE)
match = regex.search("ÿÿ?ð?öÂty")
if match:
result = match.group()
print result
else:
result = "No match found"
print result

it return "no match found" even if the two string are equal.

For equal strings you should get a match:
zäöü

For case ignorance your best bet is unicode:
<_sre.SRE_Match object at 0x401e09f8>

Peter
 
T

TOXiC

Thx it work perfectly.
If I want to query a file stream?

file = open(fileName, "r")
text = file.read()
file.close()

regex = re.compile(u"(ÿÿ‹ð…öÂ)", re.IGNORECASE)
match = regex.search(text)
if (match):
result = match.group()
print result
WritePatch(fileName,text,result)
else:
result = "No match found"
print result

It return "no match found" (the file contain the string "ÿÿ‹ð…öÂ"
but...).
Thanks in advance for the help!
 
P

Peter Otten

TOXiC said:
Thx it work perfectly.
If I want to query a file stream?

file = open(fileName, "r")
text = file.read()
file.close()

Convert the bytes read from the file to unicode. For that you have to know
the encoding, e. g.

file_encoding = "utf-8" # replace with the actual encoding
text = text.decode(file_encoding)
regex = re.compile(u"(ÿÿ‹ð…öÂ)", re.IGNORECASE)
match = regex.search(text)
if (match):
result = match.group()
print result
WritePatch(fileName,text,result)
else:
result = "No match found"
print result

It return "no match found" (the file contain the string "ÿÿ‹ð…öÂ"
but...).
Thanks in advance for the help!

Peter
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top