-1

What is the regular expression for matching a sentence that contains metacharacters ?

Following the senario :

I have a string : line = "He finally answer (after taking time to think (5 minutes) * sighs ; end)"

I want to match this part of the sentence : line2 = "after taking time to think (5 minutes) * sighs ; end"

But i don't want to use a regex to find the element inside of parentheses

For that I am using : re.findall(re.escape(line2), re.escape(line))

But I'm getting an empty result

Julia
  • 11
  • 2

1 Answers1

0

No need to execute the escape method over text(variable line in this case) you are providing:

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']
hitesh bedre
  • 459
  • 2
  • 11