Simple Python REGEX Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnny

    #1

    Simple Python REGEX Question

    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?

  • Gary Herron

    #2
    Re: Simple Python REGEX Question

    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

    Comment

    • John Machin

      #3
      Re: Simple Python REGEX Question

      On May 12, 2:21 am, Gary Herron <gher...@island training.comwro te:
      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




      Comment

      • Steven D'Aprano

        #4
        Re: Simple Python REGEX Question

        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("Mis sing bracket")
        if p q: raise ValueError("Clo se 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.

        Comment

        • James T. Dennis

          #5
          Re: Simple Python REGEX Question

          johnny <rampeters@gmai l.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='adfasd fafd(3.14159265 )asdfasdfadsfas f'
          >>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(l ine):
          pass
          # do whatever with all these numbers


          --
          Jim Dennis,
          Starshine: Signed, Sealed, Delivered

          Comment

          Working...