regular expression for getting content between parentheses

Emile van Sebille emile at fenx.com
Thu May 7 20:14:59 EDT 2009


On 5/7/2009 4:51 PM Rajanikanth Jammalamadaka said...
> Hi
> 
> I have a text file as follows:
> 
> testName = (
>  someParam = value1
>  anotherParam = (value2, value3)
> )
> 
> how do I write a regular expression to get all the contents of the
> file which are between the first and last parentheses.
> 
> In this case,  I want:
> 
>  someParam = value1
>  anotherParam = (value2, value3)
> 
> Thanks,

It's not a regex nor probably what you want, but I'd start with...


 >>> filetext = '''testName = (
...  someParam = value1
...  anotherParam = (value2, value3)
... )'''
 >>>
 >>> print filetext.split("(",1)[-1].rsplit(")",1)[0]

  someParam = value1
  anotherParam = (value2, value3)

 >>>

Emile




More information about the Python-list mailing list