RubberDuckProducer
RubberDuckProducer

Reputation: 247

Match everything until parenthesis with digits inside

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

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You can capture in a group as least as possible chars until the first occurrence of only digits between parenthesis.

^(.+?)\s*\(\d+\)

Regex demo | Python demo

import re

pattern = re.compile(r'^(.+?)\s*\(\d+\)')
text = 'Graduate (Degree-seeking) (2)'
print(pattern.findall(text))

Output

['Graduate (Degree-seeking)']

Upvotes: 3

Related Questions