0

Sorry, I don't know how to name this thread.

I need to extract the first layer of () with "" from string but when I try to use re it only returns everything in between first and second ().

import re

string = 'this is a test ("string with (some) paranthesis") how great'
extract = re.findall('\((.*?)\)', string)

print(extract)

Output:

['"string with (some']

But I need:

['"string with (some) paranthesis"']

or without the "".

Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132

4 Answers4

2

Remove the ?:

import re

string = 'this is a test ("string with (some) paranthesis") how great'
extract = re.findall(r'\((.*)\)', string)

print(extract)

The ? was not redundant because you already have .* to match everything between parentheses; it makes the match non-greedy, meaning that .* will match the shortest sequence of characters ending with parentheses, which is ("string with (some).

Elijah Cox
  • 106
  • 7
2

Use a greedy quantifier instead of a lazy one:

extract = re.findall('\((.*)\)', string)
Paolo
  • 21,270
  • 6
  • 38
  • 69
1

Depending how specific you need to be, you can try the following:

import re

string = 'this is a test ("string with (some) paranthesis") how great'
extract = re.findall(r'\((".*?")\)', string)

The pattern '\((.*)\)' will not work correctly if there are subsequent parentheses in the string e.g.

string = 'this is a test ("string with (some) paranthesis") how (really) great'

Then, depending on your strings r'\((".*?")\)' may be not appropriate too.

bb1
  • 7,174
  • 2
  • 8
  • 23
0

Perhaps a simple split would be sufficient for your needs:

string  = 'this is a test ("string with (some) paranthesis") how great'

extract = string.split("(",1)[-1].rsplit(")",1)[0]

print(extract)
# "string with (some) paranthesis"
Alain T.
  • 40,517
  • 4
  • 31
  • 51