Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex to get all text until a (, and get text inside brackets

Tags:

python

regex

I need help with two regex operations.

  1. Get all text until an open bracket.

    e.g. 'this is so cool (234)' => 'this is so cool'

  2. Get the text inside the brackets, so the number '234'

like image 310
Blankman Avatar asked Oct 20 '25 13:10

Blankman


1 Answers

Up until the paren: regex = re.compile("(.*?)\s*\(")

Inside the first set of parens: regex = re.compile(".*?\((.*?)\)")

Edit: Single regex version: regex = re.compile("(.*?)\s*\((.*?)\)")

Example output:

>>> import re
>>> r1 = re.compile("(.*?)\s*\(")
>>> r2 = re.compile(".*?\((.*?)\)")
>>> text = "this is so cool (234)"
>>> m1 = r1.match(text)
>>> m1.group(1)
'this is so cool'
>>> m2 = r2.match(text)
>>> m2.group(1)
'234'
>>> r3 = re.compile("(.*?)\s*\((.*?)\)")
>>> m3 = r3.match(text)
>>> m3.group(1)
'this is so cool'
>>> m3.group(2)
'234'
>>> 

Note of course that this won't work right with multiple sets of parens, as it's only expecting one parenthesized block of text (as per your example). The language of matching opening/closing parens of arbitrary recurrence is not regular.

like image 54
eldarerathis Avatar answered Oct 23 '25 02:10

eldarerathis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!