473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1547
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

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

Similar topics

3
by: Stefan Quandt | last post by:
I want to set a modules global variable from outside like this: <begin a.py> v = None def setv( x ): v = x def getv(): return v <end a.py> <begin b.py>
4
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program:...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
8
by: Floris van Haaster | last post by:
Hi All! I have a question, i have a web application and I store some member information in a variable i declared in a module like: Public some_info_variable as string in module1.vb But...
2
by: valinor | last post by:
Hi guys, (rather lengthy...) I'm trying to speed up the time spent on a postfilter for video. YUV 4:2:0 data, each pixel is 1 byte (0-255) The basic idea is to filter one pixel on each side...
6
by: RThaden | last post by:
Hi together, I am a bit clueless about the following problem: In the following code excerpt std::string getStr() { std::string bla="asdfsa";
8
by: Remington | last post by:
I am using windows 2000pro with access 2000. I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select...
2
by: barronmo | last post by:
I'm trying to get the difference in dates using the time module rather than datetime because I need to use strptime() to convert a date and then find out how many weeks and days until that date. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.