470,632 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

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

The beginning of the script goes like this.

Expand|Select|Wrap|Line Numbers
  1. import urllib2
  2. request = urllib2.urlopen('http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2000')
  3. source= request.read()
  4. import re
  5. name = re.compile(r'size=\"\+1\"\>\<b\>(\d+) (\w+)')
  6. thename = name.search(source)
Now I want to subtract 1 from as well as add 1 to
Expand|Select|Wrap|Line Numbers
  1. thename.group(1)
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:

Expand|Select|Wrap|Line Numbers
  1. list = re.compile(r'size=\"\+1\"\>\<b\>[2001-3000] \w+')
Mar 22 '08 #1
3 1383
bvdet
2,851 Expert Mod 2GB
The beginning of the script goes like this.

Expand|Select|Wrap|Line Numbers
  1. import urllib2
  2. request = urllib2.urlopen('http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2000')
  3. source= request.read()
  4. import re
  5. name = re.compile(r'size=\"\+1\"\>\<b\>(\d+) (\w+)')
  6. thename = name.search(source)
Now I want to subtract 1 from as well as add 1 to
Expand|Select|Wrap|Line Numbers
  1. thename.group(1)
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:

Expand|Select|Wrap|Line Numbers
  1. list = re.compile(r'size=\"\+1\"\>\<b\>[2001-3000] \w+')
This pattern will match a number in the range 2001-3000:
Expand|Select|Wrap|Line Numbers
  1. re.compile(r'size=\"\+1\"\>\<b\>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
Expand|Select|Wrap|Line Numbers
  1. source = source.replace(thename.group(1),str(int(thename.group(1))+1))
  2.  
  3. patt = re.compile(r'size=\"\+1\"\>\<b\>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
  4. m = patt.search(source)
  5. print m.group(1)
Mar 22 '08 #2
This pattern will match a number in the range 2001-3000:
Expand|Select|Wrap|Line Numbers
  1. re.compile(r'size=\"\+1\"\>\<b\>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
Expand|Select|Wrap|Line Numbers
  1. source = source.replace(thename.group(1),str(int(thename.group(1))+1))
  2.  
  3. patt = re.compile(r'size=\"\+1\"\>\<b\>(200[1-9]|2[1-9][0-9][0-9]|3000) (\w+)')
  4. m = patt.search(source)
  5. print m.group(1)
That works, thanks again. The range should look like this, though:

Expand|Select|Wrap|Line Numbers
  1.  
  2. re.compile(r'size=\"\+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+')
  3.  
as your solution did not match the range 2010-2099.
Mar 24 '08 #3
bvdet
2,851 Expert Mod 2GB
That works, thanks again. The range should look like this, though:

Expand|Select|Wrap|Line Numbers
  1.  
  2. re.compile(r'size=\"\+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+')
  3.  
as your solution did not match the range 2010-2099.
Oops! Thanks for posting back and the correction.
Mar 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Stefan Quandt | last post: by
4 posts views Thread by Steve Holden | last post: by
5 posts views Thread by Steve Holden | last post: by
8 posts views Thread by Floris van Haaster | last post: by
2 posts views Thread by valinor | last post: by
6 posts views Thread by RThaden | last post: by
2 posts views Thread by barronmo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.