cfelling at iae.nl
Sat Feb 10 14:42:20 EST 2001
Ingo 'resISt' Siebert <res1st at gmx.de> wrote:
> i´m very new to Python but i have to learn that. So i´m starting with litte
> programs but i have now a big problem.
Welcome aboard, and let my tell you right away:
often problems with Python are not big at all:)
Just a matter of picking the right modules (re in this case).
> I have (Win-)Files which contains something like
...<clear problem description snipped>
> So I wrote the following Program.
...<program with dreaded read/write file snipped
A different (and to me simpler) approach would be:
# First we create a matching machine that matches tchat, TCHAT, tChAt
# and all other casings of tchat. It will look for occurences of tchat
# followed by a space followed by anything inside double quotes. The
# use of ( and ) tells the match machine to group what's in between
# those braces (here anything but a double quote) and set it aside for
# later referencing.
match_tchat = re.compile(r'tchat "([^"]*)"', re.IGNORECASE)
# We will need an auxiliar function that will allow us to define the
# replacement string in relation to the string to be replaced. This
# function will be passed an match-object that will tell what was
# matched if asked so with the group(1) method call.
return "*" * len(match_obj.group(1))
# Now we read the whole file at once, and use the matcher
# machinery to replace all tchat's arguments in one big sweep.
# All that's left is to write this beast to disk again.
data = open("c:/tmp/test.dat").read()
data = match_tchat.sub(replacer, data)
More information about the Python-list