Find and replace in a file with regular expression

2007-01-30 Thread TOXiC
Hi everyone,
First I say that I serched and tryed everything but I cannot figure 
out how I can do it.
I want to open a a file (not necessary a txt) and find and replace a 
string.
I can do it with:

import fileinput, string, sys
fileQuery = "Text.txt"
sourceText = '''SOURCE'''
replaceText = '''REPLACE'''
def replace(fileName, sourceText, replaceText):

file = open(fileName, "r")
text = file.read() #Reads the file and assigns the value to a 
variable
file.close() #Closes the file (read session)
file = open(fileName, "w")
file.write(text.replace(sourceText, replaceText))
file.close() #Closes the file (write session)
print "All went well, the modifications are done"

replacemachine(fileQuery, sourceText, replaceText)

Now all went ok but I'm wondering if it's possible to replace text if /
sourceText/ match a regex.
Help me please!
Thx in advance

-- 
http://mail.python.org/mailman/listinfo/python-list


Regex with ASCII and non-ASCII chars

2007-01-31 Thread 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 :)

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Regex with ASCII and non-ASCII chars

2007-01-31 Thread 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!

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Regex with ASCII and non-ASCII chars

2007-01-31 Thread TOXiC
It wont work with utf-8,iso or ascii...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regex with ASCII and non-ASCII chars

2007-01-31 Thread TOXiC
On 31 Gen, 17:30, "TOXiC" <[EMAIL PROTECTED]> wrote:
> It wont work with utf-8,iso or ascii...

I think the best way is to search hex value in the file stream but I
tryed (in the regex) \hxx but it don't work...

-- 
http://mail.python.org/mailman/listinfo/python-list


Help me with this!!!

2007-02-01 Thread TOXiC
Hi all, I've this code:

regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
(\xC6\x44\x24)",re.IGNORECASE)
file = open(fileName, "rb")
for line in file:
  if (match):
  print line
file.close()

It search a text inside that hex value.
It works perfecly on a txt file but if I open a binary file (.exe,.bin
ecc...) with the same value it wont work, why?
Please help!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help me with this!!!

2007-02-01 Thread TOXiC

Peter Otten ha scritto:

> TOXiC wrote:
>
> > Hi all, I've this code:
>
> No you don't.
>

!

> > regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
> > (\xC6\x44\x24)",re.IGNORECASE)
> > file = open(fileName, "rb")
> > for line in file:
> >   if (match):
> >   print line
> > file.close()
> >
> > It search a text inside that hex value.
> > It works perfecly on a txt file but if I open a binary file (.exe,.bin
> > ecc...) with the same value it wont work, why?
> > Please help!
>
> Because the pattern isn't in the file, perhaps.
>

This pattern IS in the file (I made it and I double check with an hex
editor).
It display the file correcltly (print line) but...

-- 
http://mail.python.org/mailman/listinfo/python-list