Find a number between 2 regular expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jvdb
    New Member
    • Jul 2008
    • 4

    #1

    Find a number between 2 regular expressions

    Hello,

    I'm pretty new to Python.

    Can someone please show me how to find a number between 2 regular expressions?
    Something like this:

    Text:
    ' MyText (mymessage): 0 (Time 0 hours)'
    ' MyText (mymessage): 344 (Time 34 hours)'
    ' MyText (mymessage): 1000 (Time 2000 hours)'

    I'd like to "grep" for:
    0
    344
    1000

    And then later grep for:
    0
    34
    2000

    But am sure I will manage the second one when I can figure out how to do the first one.

    I've played with the "re" function, but the output looks like this:
    [' 0 ']

    And I can't figure out how to get rid of the white space and quotes. The plan is to get the integer and compare that with another integer.

    Thank you
  • jvdb
    New Member
    • Jul 2008
    • 4

    #2
    That output from my "re" did not post right, there are white spaces before the first quote and the 0.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      You can use the string function strip() to remove all whitespace from it (or lstrip()/rstrip() for just one end). As to the regex, what's your actual regex pattern?

      Comment

      • jvdb
        New Member
        • Jul 2008
        • 4

        #4
        Hello,

        Thanks for the reply.

        This is my regexp:

        s=' MyText (mymessage): 1000 (Time 2000 hours)'
        pattern = re.compile(r':( .*?)\(')

        pattern.findall (s)

        I'm still playing with the pattern to try and figure out how the whole thing works.

        Cheers

        Comment

        • jlm699
          Contributor
          • Jul 2007
          • 314

          #5
          Originally posted by jvdb
          pattern = re.compile(r':( .*?)\(')
          Seeing that there is a space in your text on either side of the number, you should add spaces to your pattern so that it is like this:
          [code=python]
          pattern = re.compile(r': (.*?) \(')
          [/code]

          Comment

          • jvdb
            New Member
            • Jul 2008
            • 4

            #6
            Thanks for that.

            There won't always be only 1 space on either side though.

            My output looks something like this:

            >>> s=' MyText (mymessage): 100 (Time 2000 hours)'
            >>> pattern = re.compile(r':( .*?)\(')
            >>> pattern.findall (s)
            [' 100 ']


            How do I get rid of the annoying [' and '] ?

            I want to be able to say, x=100, compare that to y.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              I would probably do something like this:
              [CODE=Python]import re

              s1 = 'MyText (mymessage): 0 (Time 0 hours)'
              s2 = 'MyText (mymessage): 344(Time 34 hours)'
              s3 = 'MyText (mymessage):100 0 (Time 2000 hours)'

              patt = re.compile(r'\) : *([0-9]+) *\(')


              for s in [s1,s2,s3]:
              m = patt.search(s)
              if m:
              print int(m.group(1))[/CODE]The pattern in parentheses (one or more digits, matching as many as possible) is saved as matchObj.group( 1). You can have no spaces or multiple spaces surrounding the digits.

              Comment

              • ripat
                New Member
                • Jul 2008
                • 1

                #8
                Hi,

                This is my first post on this forum and my very first attempt in Python. So forgive me if my answer is not to the point.

                Code:
                import re
                
                s="""
                MyText (mymessage):   0   (Time 0 hours)
                MyText (mymessage): 344(Time 34 hours)
                MyText (mymessage):1000 (Time 2000 hours)
                """
                
                patt = re.compile(r':[^0-9]*([0-9]+)[^0-9]+([0-9]+)')
                
                print patt.findall(s)
                This returns:
                Code:
                [('0', '0'), ('344', '34'), ('1000', '2000')]

                Comment

                Working...