Reputation: 41705
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.
Upvotes: 2
Views: 460
Reputation: 5384
I think your regular expression is off:
r = re.compile(r'(self.uid\s*=\s*5EFF837F-EFC2-4c32-A3D4\s*)(\S+)')
Should be:
r = re.compile(r"(self\.uid\s*=\s*'5EFF837F-EFC2-4c32-A3D4-)([^']*)'")
Then, when you have a match, grab group 1 and assign it to a variable and append your replacement string to it.
The ([^']*)
group will search for any character up to the '
mark. That's your target remove group.
Edit: June 11th, 2010 @ 2:27 EST: Justin Peel has a good point. You can do a straight search and replace with this data. Unless you are looking for the pattern of 8 characters, followed by 4, 4, 4, and 12... In which case you could use the pattern:
r = re.compile(r"self\.uid\s*=\s*('\w{8}-(:?\w{4}-){3})(\w{12})'")
Upvotes: 1
Reputation: 47092
Why are you using a regex for such a straight forward substitution?
Couldn't you just use
for l in read_file:
l.replace("5EFF837F-EFC2-4c32-A3D4-D15C7F9E1F22",
"5EFF837F-EFC2-4c32-A3D4-RHUI5345JO")
# Write to file..
or is there more to the story than you're telling us? Also, unless it is a too big of a file, I would recommend reading the whole file into a string and doing just one replace on it for the sake of speed.
Upvotes: 3