Julia
Julia

Reputation: 11

Matching string that contain Metacharacters with re.findall() and re.escape()?

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

Upvotes: -1

Views: 147

Answers (1)

hitesh bedre
hitesh bedre

Reputation: 589

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']

Upvotes: 0

Related Questions