Replace unknow string varible in file.

r0g aioe.org at technicalbloke.com
Tue Feb 10 17:00:44 EST 2009


namire wrote:
> Just as a comparison in the Windows OS this seems easy to do when
> managing files, say for the file a-blah-b-blah.tmp where blah is an
> unknown you can use: del a-*-b-*.tmp to get rid of that file. But for
> python and a string in text file I don't have a clue. @_@ could
> someone please help me?

Hi Namire,

The equivalent thing in programming languages is "Regular Expressions",
also known as "regex". It's like a small pattern matching sub-language.

There quite a lot more to it than the odd "*" so it might take a bit of
googling around and study to really understand it but the principle is
the same.

In python you need to import the 're' module.

Have a look at this, the replace method is called 'sub'...

http://www.amk.ca/python/howto/regex/regex.html

Take your time with it though, it can be confusing until you get used to
it and when you're building an expression don't try and do it all at
once, start small and build it up a little at a time.

Roger Heathcote.



More information about the Python-list mailing list