473,396 Members | 2,076 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.

Matching zero only once using RE

Hi,

I've looked at a lot of pages on the net and still can't seem to nail
this. Would someone more knowledgeable in regular expressions please
provide some help to point out what I'm doing wrong?

I am trying to see if a web page contains the exact text:
You have found 0 matches

But instead I seem to be matching all sorts of expected line like
You have found <a number up to 5 digits long with comma> matches
for example:
You have found 34 matches
You have found 189 matches
You have found 16,734 matches
You have found 1,706 matches
You have found 300 matches

The last 2 I thought I had eliminated but sadly it seems not the
examples above actually seem to match my expression below. :(

Here is what I'm doing:
zeromatch = []
SecondarySearchTerm = 'You found (0){1} matches'
primarySearchTerm = 'Looking for Something'
primarySearchTerm2 = 'has been an error connecting'

# pagetext is all the body text on a web page.
# I'm using COM to drive MSIE and pagetext = doc.body.outerText

if (re.search(primarySearchTerm, pagetext) or
re.search(primarySearchTerm2, pagetext)):
failedlinks.append(link)
elif (re.search(SecondarySearchTerm, pagetext)):
zeromatch.append(link)

I've tried other RE's be had even more spectacular failures any help
would be greatly appreciated.

Thanks in Advance,
Greg Moore
Software Test
Shop.com

Oct 7 '05 #1
7 1460
On 7 Oct 2005 10:35:22 -0700, GregM <gr***@taming-tech.com> wrote:
Hi,

I've looked at a lot of pages on the net and still can't seem to nail
this. Would someone more knowledgeable in regular expressions please
provide some help to point out what I'm doing wrong?

I am trying to see if a web page contains the exact text:
You have found 0 matches

Shouldn't your regular expression be "You have found 0 matches". If
you're looking for that exact string, then you should use that.

This works for me:
s = "asdfasfdfs You have found 0 matches asdfasdfasdf"
import re
re.search("You have found 0 matches", s)

<_sre.SRE_Match object at 0x00B21800>

ALso, it looks like your pattern is off. The pattern you use is given below...

SecondarySearchTerm = 'You found (0){1} matches'

However, you state that you are looking for 'You have found 0 matches'

* Notice the 'have' in the string you are searching for and it's
absence in your search term ;)

HTH,
jw
Oct 7 '05 #2
GregM wrote:
I am trying to see if a web page contains the exact text:
You have found 0 matches


It is unclear to me why you're using a regex at all. If you want to
find the *exact* text "You have found 0 matches" perhaps you should do
something like this:

if "You have found 0 matches" in pagetext:
print 'yes'
else:
print 'no'

--
Benji York

Oct 7 '05 #3
"GregM" <gr***@taming-tech.com> writes:
I've looked at a lot of pages on the net and still can't seem to nail
this. Would someone more knowledgeable in regular expressions please
provide some help to point out what I'm doing wrong?

I am trying to see if a web page contains the exact text:
You have found 0 matches


Why in the gods names are you using an re for this? Just use in:
pretext1 = 'This is some text with You have found 0 matches in it'
pretext2 = 'This text should not match'
'You have found 0 matches' in pretext1 True 'You have found 0 matches' in pretext2 False


I think it's time to form a Committee for the Prevention of Regular
Expression Abuse.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 7 '05 #4
Hi
thanks to all of you. Mike I like your committee idea. where can I
join? lol

Greg.

Oct 7 '05 #5
In article <86************@bhuda.mired.org>, Mike Meyer <mw*@mired.org> wrote:

I think it's time to form a Committee for the Prevention of Regular
Expression Abuse.


'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.' --Jamie Zawinski
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair
Oct 7 '05 #6
Mike Meyer wrote:
I think it's time to form a Committee for the Prevention of Regular
Expression Abuse.


on the other hand, the RE engine uses a more advanced scanning
algorithm than string find, which means that constant RE:s can in
fact be faster under some circumstances (certain patterns, target
strings with lots of false partial matches, etc).

see "searching for literal text" on this page

http://mail.python.org/pipermail/pyt...st/007797.html

for some figures.

(things have improved since then, especially in 2.4. in 2.3, "in" was
also a lot slower than "find". and all three are still slower than they
have to be: http://online.effbot.org/2004_08_01_archive.htm#find2 )

</F>

Oct 7 '05 #7
Mike Meyer <mw*@mired.org> wrote:

I think it's time to form a Committee for the Prevention of Regular
Expression Abuse.


As I learned from personal experience, this is a disease which one
contracts when moving to Python from Perl. Perl teaches you that the
entire world is a string, and every operation is a regular expression match
upon that string.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 9 '05 #8

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

Similar topics

1
by: David C. Barber | last post by:
I'm trying to determine if any matching records exist on a LIKE query performing a partial match of last names to a remote back-end database in the most efficient manner possible. LAN Traffic...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
12
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1....
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
8
by: 116Rohan | last post by:
I came across a question in one of the Computing olympiad regarding string pattern matching. Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the...
20
by: Serge Rielau | last post by:
I'm playing with the ICU library (slow weekend :-) and it's quite simple to expose the regular expression functionality for Unicode databases. Presuming that the majority is not on unicode, would...
5
by: DBC User | last post by:
How would you develop a zero footprint application, is the smart client application a zero footprint application? Thanks.
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
13
by: comcraft1966 | last post by:
I am planning an Access database for a Hotel chain with several hotels all over the UK. The required functionality is as follows: 1. A client wants to know the nearest hotel/s to a specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
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.