Simple Python REGEX Question

J

johnny

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?
 
G

Gary Herron

johnny said:
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
 
J

John Machin

johnny said:
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
 
S

Steven D'Aprano

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]
 
J

James T. Dennis

johnny said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top