473,396 Members | 1,865 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,396 software developers and data experts.

Where can be a problem?

Lad
I use the following
###############
import re
Results=[]
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print Results
#############
to extract from data1 all numbers such as 15015,15016,15017

But the program extracts only the last number 15017.
Why?
Thank you for help
La.

Aug 12 '05 #1
5 1077
Lad wrote:
I use the following
###############
import re
Results=[]
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print Results
#############
to extract from data1 all numbers such as 15015,15016,15017

But the program extracts only the last number 15017.
Why?
Thank you for help
La.


After changing

data = '...
'

to

data = '''...
'''

I get all three numbers. There is probably another significant difference
between the posted code and the code you are actually running.

Peter

Aug 12 '05 #2
Lad
Peter,
I tried exactly this
########
import re
Results=[]
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print "Results are= ",Results
#########
and received
Results are= ['15017']

Not all numbers

What exactly did you get?
Thanks.
L.

Aug 12 '05 #3
Lad wrote:
Peter,
I tried exactly this
########
import re
Results=[]
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print "Results are= ",Results
#########
and received
Results are= ['15017']

Not all numbers

What exactly did you get?


With /exactly/ this, I get:

$ cat lad1.py
import re
Results=[]
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print "Results are= ",Results
$ python lad1.py
File "lad1.py", line 3
data1='<a href="detailaspxmember=15015&mode=advert" </a><a
^
SyntaxError: EOL while scanning single-quoted string

When I modify it to compile, I get /exactly/ this:

$ cat lad2.py
import re
Results=[]
data1='''<a href="detailaspxmember=15015&mode=advert" </a><a
href="detailaspxmember=15016&mode=advert" </a><a
href="detailaspxmember=15017&mode=advert" </a>'''
ID = re.compile(r'^.*=(\d+)&.*$',re.MULTILINE)
Results=re.findall(ID,data1)
print "Results are= ",Results
$ python lad2.py
Results are= ['15015', '15016', '15017']

Peter

Aug 12 '05 #4
Lad
Thank you Peter for help.
The reason why it did not work was the fact that findall function
required CRLF among lines

Aug 12 '05 #5
Try this, its a bit more readable than your re.

from pyparsing import Word,nums,Literal,replaceWith

data1='''<a href="detailaspxmember=15015&m-ode=advert" </a><a
href="detailaspxmember=15016&m*ode=advert" </a><a
href="detailaspxmember=15017&m*ode=advert" </a>'''

# a number is a word composed of nums, that is, the digits 0-9
# your search string is looking for a number between an '=' and '&'
EQUALS = Literal("=")
AMPER = Literal("&")
number = Word(nums)
hrefNumber = EQUALS + number + AMPER

# scanString is a generator, that returns matching tokens, start,
# and end location for each occurrence in the input string - we
# just care about the second token of each match
print [ tokens[1] for tokens,s,e in hrefNumber.scanString(data1) ]

# just for grins, here is how to convert the numbers to the
# string "###"
number.setParseAction( replaceWith("###") )
print number.transformString(data1)
Prints:

['15015', '15016', '15017']
<a href="detailaspxmember=###&m-ode=advert" </a><a
href="detailaspxmember=###&m*ode=advert" </a><a
href="detailaspxmember=###&m*ode=advert" </a>

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul

Aug 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
2
by: BoB Teijema | last post by:
Hi all, One of our companies is having problems with a query on a linked server. They have two servers, serverA and serverB. On serverA they have set up a linked server to serverB. Query:...
16
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a...
10
by: Ray Stevens | last post by:
I am attempting to test a VeriSign account and the setup instructions stated that the certs file needed to go into the Windows\System32 folder. I am getting an error from the code-behind assebly...
2
by: jaYPee | last post by:
I have no problem setting the selectcommand in sqldataadapter to fetch record from sqlserver w/ where clause in parent table. however, my problem is on how can i fetch the child table which is...
0
by: Matthieu Siggen | last post by:
Hello, I'm really confused about how to define services when concerning data management. I'm going to take an example to show where is my problem. If I'm developping an application with two...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
7
by: Chris | last post by:
Hello all... I have a program with the following structure (all classes mentioned are of my own creation, and none of the classes contain try or catch blocks): - main() consists of a large...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
by: ssmeshack | last post by:
Hai all, I have problem here. Im using VWD with C#. Database Sql Server 2005. I have done auto rotation for staffname where autorotation = 1. It was no problem until I add a new user with...
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.