-1

匹配包含元字符的句子的正则表达式是什么?

在 senario 之后:

我有一个字符串:line = "He finally answer (after taking time to think (5 minutes) * sighs ; end)"

我想匹配这部分句子:line2 = "after taking time to think (5 minutes) * sighs ; end"

但我不想使用正则表达式来查找括号内的元素

为此,我正在使用:re.findall(re.escape(line2), re.escape(line))

但我得到一个空的结果

4

1 回答 1

0

无需对您提供的文本(在这种情况下为可变行)执行转义方法:

import re

line = "He finally answer (after taking time to think (5 minutes) * sighs ; end)"

line2 = "after taking time to think (5 minutes) * sighs ; end" 

result = re.findall(re.escape(line2), line)

print(result) # ['after taking time to think (5 minutes) * sighs ; end']
于 2021-09-07T19:26:25.127 回答