How to subtract/add from/to a value found using the RE module?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nihilium
    New Member
    • Mar 2008
    • 16

    How to subtract/add from/to a value found using the RE module?

    The beginning of the script goes like this.

    [CODE=python]import urllib2
    request = urllib2.urlopen ('http://ssd.jpl.nasa.go v/sbdb.cgi?sstr=2 000')
    source= request.read()
    import re
    name = re.compile(r'si ze=\"\+1\"\>\<b \>(\d+) (\w+)')
    thename = name.search(sou rce)[/CODE]

    Now I want to subtract 1 from as well as add 1 to [CODE=python]thename.group(1 )[/CODE]

    which always has a numerical value.

    ----

    Another thing I don't get how to implement in regex, is how to match ranges such as 2001-3000. I tried using the following, which apparently did not work:

    [CODE=python]list = re.compile(r'si ze=\"\+1\"\>\<b \>[2001-3000] \w+')[/CODE]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by nihilium
    The beginning of the script goes like this.

    [CODE=python]import urllib2
    request = urllib2.urlopen ('http://ssd.jpl.nasa.go v/sbdb.cgi?sstr=2 000')
    source= request.read()
    import re
    name = re.compile(r'si ze=\"\+1\"\>\<b \>(\d+) (\w+)')
    thename = name.search(sou rce)[/CODE]

    Now I want to subtract 1 from as well as add 1 to [CODE=python]thename.group(1 )[/CODE]

    which always has a numerical value.

    ----

    Another thing I don't get how to implement in regex, is how to match ranges such as 2001-3000. I tried using the following, which apparently did not work:

    [CODE=python]list = re.compile(r'si ze=\"\+1\"\>\<b \>[2001-3000] \w+')[/CODE]
    This pattern will match a number in the range 2001-3000:[code=Python]re.compile(r'si ze=\"\+1\"\>\<b \>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')[/code][code=Python]
    source = source.replace( thename.group(1 ),str(int(thena me.group(1))+1) )

    patt = re.compile(r'si ze=\"\+1\"\>\<b \>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
    m = patt.search(sou rce)
    print m.group(1)[/code]

    Comment

    • nihilium
      New Member
      • Mar 2008
      • 16

      #3
      Originally posted by bvdet
      This pattern will match a number in the range 2001-3000:[code=Python]re.compile(r'si ze=\"\+1\"\>\<b \>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')[/code][code=Python]
      source = source.replace( thename.group(1 ),str(int(thena me.group(1))+1) )

      patt = re.compile(r'si ze=\"\+1\"\>\<b \>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
      m = patt.search(sou rce)
      print m.group(1)[/code]
      That works, thanks again. The range should look like this, though:

      [CODE=Python]

      re.compile(r'si ze=\"\+1\"\>\<b \>(2[0-9][0-9][1-9]|2[1-9][0-9][0-9]|2[0-9][1-9][0-9]|3000) \w+')
      [/CODE]

      as your solution did not match the range 2010-2099.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by nihilium
        That works, thanks again. The range should look like this, though:

        [CODE=Python]

        re.compile(r'si ze=\"\+1\"\>\<b \>(2[0-9][0-9][1-9]|2[1-9][0-9][0-9]|2[0-9][1-9][0-9]|3000) \w+')
        [/CODE]

        as your solution did not match the range 2010-2099.
        Oops! Thanks for posting back and the correction.

        Comment

        Working...