Use Regular expression with fileinput

Posted by chrissygormley on Stack Overflow See other posts from Stack Overflow or by chrissygormley
Published on 2010-06-15T09:04:25Z Indexed on 2010/06/15 9:12 UTC
Read the original article Hit count: 235

Filed under:
|
|

Hello,

I am trying to replace a variable stored in another file using regular expression. The code I have tried is:

r = re.compile(r"self\.uid\s*=\s*('\w{12})'")
for line in fileinput.input(['file.py'], inplace=True): 
    print line.replace(r.match(line), sys.argv[1]), 

The format of the variable in the file is:

self.uid = '027FC8EBC2D1'

I am trying to pass in a parameter in this format and use regular expression to verify that the sys.argv[1] is correct format and to find the variable stored in this file and replace it with the new variable.

Can anyone help. Thanks for the help.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex