搜索一个字符串并用一个变量替换它

时间:2022-01-17 16:49:28

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:

我正在尝试使用正则表达式搜索UUID号的文档,并用一个新数字替换它的末尾。到目前为止,我的代码是:

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:

文件测试。txt中存储了以下UUID:

self.uid = '5EFF837F-EFC2-4c32-A3D4-D15C7F9E1F22'

I want to replace the part D15C7F9E1F22.

我想更换零件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.

谢谢你的帮助。

2 个解决方案

#1


3  

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.

或者这个故事比你告诉我们的更多?此外,除非文件太大,否则我建议将整个文件读入一个字符串,并仅对其进行一次替换,以提高速度。

#2


1  

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.

然后,当您有一个匹配时,获取组1并将其分配给一个变量并将替换字符串附加到它。

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:

编辑:2010年6月11日@东部时间2:27:Justin Peel有一个很好的观点。你可以直接搜索并替换这些数据。除非您正在寻找8个字符的模式,然后是4、4、4和12……在这种情况下,您可以使用以下模式:

r = re.compile(r"self\.uid\s*=\s*('\w{8}-(:?\w{4}-){3})(\w{12})'")

#1


3  

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.

或者这个故事比你告诉我们的更多?此外,除非文件太大,否则我建议将整个文件读入一个字符串,并仅对其进行一次替换,以提高速度。

#2


1  

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.

然后,当您有一个匹配时,获取组1并将其分配给一个变量并将替换字符串附加到它。

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:

编辑:2010年6月11日@东部时间2:27:Justin Peel有一个很好的观点。你可以直接搜索并替换这些数据。除非您正在寻找8个字符的模式,然后是4、4、4和12……在这种情况下,您可以使用以下模式:

r = re.compile(r"self\.uid\s*=\s*('\w{8}-(:?\w{4}-){3})(\w{12})'")