By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

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

P: 16
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
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 16
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
Expert Mod 2.5K+
P: 2,851
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.