By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Simple Python REGEX Question

P: n/a
I need to get the content inside the bracket.

eg. some characters before bracket (3.12345).

I need to get whatever inside the (), in this case 3.12345.

How do you do this with python regular expression?

May 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
johnny wrote:
I need to get the content inside the bracket.

eg. some characters before bracket (3.12345).

I need to get whatever inside the (), in this case 3.12345.

How do you do this with python regular expression?
>>import re
x = re.search("[0-9.]+", "(3.12345)")
print x.group(0)
3.12345

There's a lot more to the re module, of course. I'd suggest reading the
manual, but this should get you started.
Gary Herron

May 11 '07 #2

P: n/a
On May 12, 2:21 am, Gary Herron <gher...@islandtraining.comwrote:
johnny wrote:
I need to get the content inside the bracket.
eg. some characters before bracket (3.12345).
I need to get whatever inside the (), in this case 3.12345.
How do you do this with python regular expression?
>import re
x = re.search("[0-9.]+", "(3.12345)")
print x.group(0)

3.12345

There's a lot more to the re module, of course. I'd suggest reading the
manual, but this should get you started.
>>s = "some chars like 987 before the bracket (3.12345) etc"
x = re.search("[0-9.]+", s)
x.group(0)
'987'

OP sez: "I need to get the content inside the bracket"
OP sez: "I need to get whatever inside the ()"

My interpretation:
>>for s in ['foo(123)bar', 'foo(123))bar', 'foo()bar', 'foobar']:
.... x = re.search(r"\([^)]*\)", s)
.... print repr(x and x.group(0)[1:-1])
....
'123'
'123'
''
None


May 12 '07 #3

P: n/a
On Fri, 11 May 2007 08:54:31 -0700, johnny wrote:
I need to get the content inside the bracket.

eg. some characters before bracket (3.12345).

I need to get whatever inside the (), in this case 3.12345.

How do you do this with python regular expression?
Why would you bother? If you know your string is a bracketed expression,
all you need is:

s = "(3.12345)"
contents = s[1:-1] # ignore the first and last characters

If your string is more complex:

s = "lots of things here (3.12345) and some more things here"

then the task is harder. In general, you can't use regular expressions for
that, you need a proper parser, because brackets can be nested.

But if you don't care about nested brackets, then something like this is
easy:

def get_bracket(s):
p, q = s.find('('), s.find(')')
if p == -1 or q == -1: raise ValueError("Missing bracket")
if p q: raise ValueError("Close bracket before open bracket")
return s[p+1:q-1]

Or as a one liner with no error checking:

s[s.find('(')+1:s.find(')'-1]
--
Steven.

May 12 '07 #4

P: n/a
johnny <ra*******@gmail.comwrote:
I need to get the content inside the bracket.
eg. some characters before bracket (3.12345).
I need to get whatever inside the (), in this case 3.12345.
How do you do this with python regular expression?
I'm going to presume that you mean something like:

I want to extract floating point numerics from parentheses
embedded in other, arbitrary, text.

Something like:
>>given='adfasdfafd(3.14159265)asdfasdfadsfasf'
import re
mymatch = re.search(r'\(([0-9.]+)\)', given).groups()[0]
mymatch
'3.14159265'
>>>
Of course, as with any time you're contemplating the use of regular
expressions, there are lots of questions to consider about the exact
requirements here. What if there are more than such pattern? Do you
only want the first match per line (or other string)? (That's all my
example will give you). What if there are no matches? My example
will raise an AttributeError (since the re.search will return the
"None" object rather than a match object; and naturally the None
object has no ".groups()' method.

The following might work better:
>>mymatches = re.findall(r'\(([0-9.]+)\)', given).groups()[0]
if len(mymatches):
...
... and, of couse, you might be better with a compiled regexp if
you're going to repeast the search on many strings:

num_extractor = re.compile(r'\(([0-9.]+)\)')
for line in myfile:
for num in num_extractor(line):
pass
# do whatever with all these numbers
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

May 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.