Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
regular expression for getting content between parentheses
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Emile van Sebille  
View profile  
 More options May 7 2009, 8:14 pm
Newsgroups: comp.lang.python
From: Emile van Sebille <em...@fenx.com>
Date: Thu, 07 May 2009 17:14:59 -0700
Local: Thurs, May 7 2009 8:14 pm
Subject: Re: regular expression for getting content between parentheses
On 5/7/2009 4:51 PM Rajanikanth Jammalamadaka said...

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rhodri James  
View profile  
 More options May 7 2009, 8:19 pm
Newsgroups: comp.lang.python
From: "Rhodri James" <rho...@wildebst.demon.co.uk>
Date: Fri, 08 May 2009 01:19:21 +0100
Local: Thurs, May 7 2009 8:19 pm
Subject: Re: regular expression for getting content between parentheses
On Fri, 08 May 2009 00:51:14 +0100, Rajanikanth Jammalamadaka  

<rajanika...@gmail.com> wrote:
> 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.

You don't, or at least you don't without some cast-iron guarantees
that your file will look *exactly* like this.  If you can guarantee
that, then this should work:

import re

f = open(filename)
data = f.read()
m = re.match(r"""[^(]*\(     # Find the first open parenthesis
                  (.*)        # Gobble up everything...
                  \)[^)]*$    # ...to the last close paren""",
              data, re.X)
if m:
   print m.group(1)

Notice that this will do *exactly* what you asked; pick up
everything between the first and the last parentheses.  In
particular, if your text looks like this:

testName1 = (
   someParam1 = value1
   anotherParam1 = (value2, value3)
)
testName2 = (
   sameParam2 = value4
)

...then what you'll get out is:

   someParam1 = value1
   anotherParam1 = (value2, value3)
)
testName2 = (
   sameParam2 = value4

You can't get around that with regular expressions, you'll
have to parse your way through the input string counting
open and close parentheses as you go.

--
Rhodri James *-* Wildebeeste Herder to the Masses


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rajanikanth Jammalamadaka  
View profile  
 More options May 7 2009, 9:25 pm
Newsgroups: comp.lang.python
From: Rajanikanth Jammalamadaka <rajanika...@gmail.com>
Date: Thu, 7 May 2009 18:25:39 -0700
Local: Thurs, May 7 2009 9:25 pm
Subject: Re: regular expression for getting content between parentheses
Thanks for your replies.

I changed the file to look like this:

{ testName : {"someParam": "value1", "anotherParam": (value2, value3)},

}

to make it look like a hash to Python.

Thanks,

Raj

On Thu, May 7, 2009 at 5:19 PM, Rhodri James

--
Rajanikanth

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »