xxx
xxx

Reputation: 15

Why does the re.sub() function is return an empty string?

I have a string

string = '(month)April(year)'

If I want to use re.sub to only end up with the month how would I do that? So far I have been using

re.sub(r'(.+)', '', string) 

but I end up with a completely empty string

Upvotes: 0

Views: 427

Answers (1)

sal
sal

Reputation: 3593

You need to escape the parenthesis, otherwise they are considered part of the regex. Something like this does the trick:

string = '(month)April(year)'
re.sub(r'\(\w+\)', '', string)
# April

See it also on https://regex101.com/r/vOjpRU/1

Upvotes: 2

Related Questions