Simple Python REGEX Question

James T. Dennis jadestar at idiom.com
Sat May 12 17:38:04 EDT 2007


johnny <rampeters at gmail.com> 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?

 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




More information about the Python-list mailing list