简体   繁体   English

查找所有出现的正则表达式模式并替换为eval输出

[英]Find all occurrences of a regex pattern and replace with eval output

I have a txt file which contains a lot of strings such as 我有一个txt文件,其中包含很多字符串,如

Chr(101)
Chr(97)
Chr(104)
...

I am using the below code to find all occurrences of such strings using regex. 我使用下面的代码使用正则表达式查找所有出现的此类字符串。 What I'd like to do is to replace each occurrence with its evaluated output. 我想做的是用它的评估输出替换每个事件。 So in this case I'd replace the above with: 所以在这种情况下,我将以上代替:

e
a
h

The code I have is as follows: 我的代码如下:

with open(oFile, "r") as f:
    for line in f:
      # find all occurrences of Chr(\d+\) and put in a list
      chrList = [str(s) for s in re.findall(r'Chr\(\d+\)', line)]
      # print chrList 
      for c in chrList:
        # print eval(c.lower())
        out = re.sub(c, eval(c.lower()), line)

If I print the eval(c.lower()) line then it outputs as expected. 如果我打印eval(c.lower())行, eval(c.lower())预期输出。 However the re.sub line fails with the following error: 但是, re.sub行失败并出现以下错误:

 raise error, v # invalid expression sre_constants.error: bogus escape (end of line) 

Not sure where I'm going wrong here. 不知道我在哪里错了。

You don't have to use distinct search and replace functions. 您不必使用不同的搜索和替换功能。 You can invoke eval using the functional form of re.sub : 您可以使用re.sub的函数形式调用eval

for line in f:
  out = re.sub(r'Chr\(\d+\)', lambda c: eval(c.group(0).lower()), line)
  print out

Your going to want to escape your search pattern because parenthesis are special characters in regular expressions . 您想要转义搜索模式, 因为括号是正则表达式中的特殊字符 You can easily do this using re.escape . 您可以使用re.escape轻松完成此操作。

out = re.sub(re.escape(c), eval(c.lower()), line)

And as an example: 作为一个例子:

strings = ['Chr(100)', 'Chr(101)', 'Chr(102)']
values = [re.sub(re.escape(c), eval(c.lower()), c) for c in strings]

# ['d', 'e', 'f']

That being said, why not just use replace() ? 话虽如此,为什么不使用replace()

out = line.replace(c, eval(c.lower())

Same thing but without eval() or imports: 同样的事情,但没有eval()或导入:

strings = ['Chr(100)', 'Chr(101)', 'Chr(102)']
values = [chr(x) for x in (int(c.replace("Chr(", "").replace(")","")) for c in strings)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM