Reputation: 247
I was trying to get the text before the text like (2)
in a pattern similar to [(\d)]
import re
pattern = re.compile(r'^[^(\d*)]+')
text = 'Graduate (Degree-seeking) (2)'
pattern.findall(text)
I only got ['Graduate ']
from the code I compiled up there.
The desired output should be ['Graduate (Degree-seeking)']
Upvotes: 1
Views: 51
Reputation: 163362
You can capture in a group as least as possible chars until the first occurrence of only digits between parenthesis.
^(.+?)\s*\(\d+\)
import re
pattern = re.compile(r'^(.+?)\s*\(\d+\)')
text = 'Graduate (Degree-seeking) (2)'
print(pattern.findall(text))
Output
['Graduate (Degree-seeking)']
Upvotes: 3