Johan Tuls
Johan Tuls

Reputation: 115

Find substring inside parentheses and single quotes

I would like to get the string that is within the parentheses and within the ''.

import re
a_string = "_TableStyle('Table Grid') id: 219287880"
result = re.findall(r'\((.*?) *\)', a_string)[0][1:-1]
print(result)

This block works. But isn't there a more elegant way?

Upvotes: 0

Views: 231

Answers (4)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use double quotes around the literal if you do not want to escape single quotes. Triple quotes are even nicer. Getting part of a regex match is simple with capturing groups:

import re
a_string = "_TableStyle('Table Grid') id: 219287880"
match = re.search(r"""\('([^']*)'\)""", a_string)
if match:
    print(match.group(1))

See Python proof.

EXPLANATION

--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  '                        '\''
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  '                        '\''
--------------------------------------------------------------------------------
  \)                       ')'

Upvotes: 0

Olvin Roght
Olvin Roght

Reputation: 7812

It's quite inefficient to use re.findall() for retrieving just first match, better to use re.search() which will return after first match found. Also you can simplify your regular expression to \('(.+?)'\).

Code:

import re
...
result = re.search(r"\('(.+?)'\)", a_string).group(1)

Upvotes: 1

Shreyan Avigyan
Shreyan Avigyan

Reputation: 183

Yes. There's another way to achieve this. Here it is,

import re
a_string = "_TableStyle('Table Grid') id: 219287880"
result = re.findall(r'\(\'(.*?) *\'\)', a_string)[0]
print(result)

This code searches also for a ' ' and would only extract the text between the (' ').

Edit:

We can also write the code like this,

import re
a_string = "_TableStyle('Table Grid') id: 219287880"
result = re.search(r'\(\'(.*?) *\'\)', a_string).group(1)
print(result)

Upvotes: 0

thibsc
thibsc

Reputation: 4049

If you have only one pair of parentheses in your line, you can do it without regex:

a_string = "_TableStyle('Table Grid') id: 219287880"

openParenthesis = a_string.index("('") + 2
closeParenthesis = a_string.index("')", openParenthesis)

# 'Table Grid'
a_string[openParenthesis:closeParenthesis]

Upvotes: 0

Related Questions