Search for a String and replace it with a variable
Posted
by chrissygormley
on Stack Overflow
See other posts from Stack Overflow
or by chrissygormley
Published on 2010-06-11T16:05:03Z
Indexed on
2010/06/11
16:22 UTC
Read the original article
Hit count: 284
Hello,
I am trying to use regular expression to search a document fo a UUID number and replace the end of it with a new number. The code I have so far is:
read_file = open('test.txt', 'r+')
write_file = open('test.txt', 'w')
r = re.compile(r'(self.uid\s*=\s*5EFF837F-EFC2-4c32-A3D4\s*)(\S+)')
for l in read_file:
m1 = r.match(l)
if m1:
new=(str,m1.group(2))
new??????
This where I get stuck.
The file test.txt
has the below UUID stored in it:
self.uid = '5EFF837F-EFC2-4c32-A3D4-D15C7F9E1F22'
I want to replace the part D15C7F9E1F22
.
I have also tried this:
r = re.compile(r'(self.uid\s*=\s*)(\S+)')
for l in fp:
m1 = r.match(l)
new=map(int,m1.group(2).split("-")
new[4]='RHUI5345JO'
But I cannot seem to match the string.
Thanks in advance for any help.
© Stack Overflow or respective owner