0

I am trying to get a text within

(German: [ˈadɔlf ˈhɪtlɐ] (About this sound listen); 20 April 1889 – 30 April 1945) in a paragraph

Expected output:

German: [ˈadɔlf ˈhɪtlɐ] (About this sound listen); 20 April 1889 – 30 April 1945

I am using:

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

But the result is coming as:

(German: [ˈadɔlf ˈhɪtlɐ] ( listen
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
coder7
  • 55
  • 1
  • 1
  • 7
  • Is the line you show *all* of the text you're matching against? Or do you more text before or after the parentheses you care about? The problem you may have is that the additional text might also contain further sets of parentheses. (e.g. `X y (foo (bar) baz). Quux (Quuux).` Many simple approaches (like regular expressions) will have a very difficult time finding matching pairs of parentheses. You may need to write your own parser. – Blckknght Jun 24 '17 at 00:19

1 Answers1

0

Your problem is that s.find(")") matches the first occurrence of ")" that it finds in s, and you want the last. you can use rfind find the last occurence of the string ")":

s = "(German: [ˈadɔlf ˈhɪtlɐ] (About this sound listen); 20 April 1889 – 30 April 1945)"

s[s.find("(")+1:s.rfind(")")]
#German: [ˈadɔlf ˈhɪtlɐ] (About this sound listen); 20 April 1889 – 30 April 1945

If you want to access the inner layer of parenthesis, just perform the previous step again:

text = s[s.find("(")+1:s.rfind(")")]
text[text.find("(")+1:text.rfind(")")]
#About this sound listen
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65