Regular expression to return text between parenthesis

PythonRegexPython 3.x

Python Problem Overview


u'abcde(date=\'2/xc2/xb2\',time=\'/case/test.png\')'

All I need is the contents inside the parenthesis.

Python Solutions


Solution 1 - Python

If your problem is really just this simple, you don't need regex:

s[s.find("(")+1:s.find(")")]

Solution 2 - Python

Use re.search(r'\((.*?)\)',s).group(1):

>>> import re
>>> s = u'abcde(date=\'2/xc2/xb2\',time=\'/case/test.png\')'
>>> re.search(r'\((.*?)\)',s).group(1)
u"date='2/xc2/xb2',time='/case/test.png'"

Solution 3 - Python

If you want to find all occurences:

>>> re.findall('\(.*?\)',s)
[u"(date='2/xc2/xb2',time='/case/test.png')", u'(eee)']

>>> re.findall('\((.*?)\)',s)
[u"date='2/xc2/xb2',time='/case/test.png'", u'eee']

Solution 4 - Python

Building on tkerwin's answer, if you happen to have nested parentheses like in

st = "sum((a+b)/(c+d))"

his answer will not work if you need to take everything between the first opening parenthesis and the last closing parenthesis to get (a+b)/(c+d), because find searches from the left of the string, and would stop at the first closing parenthesis.

To fix that, you need to use rfind for the second part of the operation, so it would become

st[st.find("(")+1:st.rfind(")")]

Solution 5 - Python

import re

fancy = u'abcde(date=\'2/xc2/xb2\',time=\'/case/test.png\')'

print re.compile( "\((.*)\)" ).search( fancy ).group( 1 )

Solution 6 - Python

contents_re = re.match(r'[^\(]*\((?P<contents>[^\(]+)\)', data)
if contents_re:
    print(contents_re.groupdict()['contents'])

Solution 7 - Python

No need to use regex .... Just use list slicing ...

string="(tidtkdgkxkxlgxlhxl) ¥£%#_¥#_¥#_¥#"
print(string[string.find("(")+1:string.find(")")])

Solution 8 - Python

Here are several ways to extract strings between parentheses in Pandas with the \(([^()]+)\) regex (see its online demo) that matches

  • \( - a ( char
  • ([^()]+) - then captures into Group 1 any one or more chars other than ( and )
  • \) - a ) char.

Extracting the first occurrence using Series.str.extract:

import pandas as pd
df = pd.DataFrame({'Description':['some text (value 1) and (value 2)']})
df['Values'] = df['Description'].str.extract(r'\(([^()]+)\)')
# => df['Values']
#    0    value 1
#    Name: Values, dtype: object

Extracting (finding) all occurrences using Series.str.findall:

import pandas as pd
df = pd.DataFrame({'Description':['some text (value 1) and (value 2)']})
df['Values'] = df['Description'].str.findall(r'\(([^()]+)\)')
# => df['Values']
#    0    [value 1, value 2]
#    Name: Values, dtype: object

df['Values'] = df['Description'].str.findall(r'\(([^()]+)\)').str.join(', ')
# => df['Values']
#    0    value 1, value 2
#    Name: Values, dtype: object

Note that .str.join(', ') is used to create a comma-separated string out of the resulting list of strings. You may adjust this separator for your scenario.

Solution 9 - Python

TheSoulkiller's answer is great. just in my case, I needed to handle extra parentheses and only extract the word inside the parentheses. a very small change would solve the problem

>>> s=u'abcde((((a+b))))-((a*b))'
>>> re.findall('\((.*?)\)',s)
['(((a+b', '(a*b']
>>> re.findall('\(+(.*?)\)',s)
['a+b', 'a*b']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser469652View Question on Stackoverflow
Solution 1 - PythontkerwinView Answer on Stackoverflow
Solution 2 - PythonwaanView Answer on Stackoverflow
Solution 3 - PythonTheSoulkillerView Answer on Stackoverflow
Solution 4 - PythonFaustoWView Answer on Stackoverflow
Solution 5 - PythonAnonymousView Answer on Stackoverflow
Solution 6 - PythontreadView Answer on Stackoverflow
Solution 7 - PythonJust don't be user123View Answer on Stackoverflow
Solution 8 - PythonWiktor StribiżewView Answer on Stackoverflow
Solution 9 - PythonfjahanView Answer on Stackoverflow