Matching zero only once using RE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GregM

    #1

    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 = []
    SecondarySearch Term = 'You found (0){1} matches'
    primarySearchTe rm = 'Looking for Something'
    primarySearchTe rm2 = '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.outerT ext

    if (re.search(prim arySearchTerm, pagetext) or
    re.search(prima rySearchTerm2, pagetext)):
    failedlinks.app end(link)
    elif (re.search(Seco ndarySearchTerm , pagetext)):
    zeromatch.appen d(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

  • Jaime Wyant

    #2
    Re: Matching zero only once using RE

    On 7 Oct 2005 10:35:22 -0700, GregM <gregm@taming-tech.com> wrote:[color=blue]
    > 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
    >[/color]
    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:
    [color=blue][color=green][color=darkred]
    >>> s = "asdfasfdfs You have found 0 matches asdfasdfasdf"
    >>> import re
    >>> re.search("You have found 0 matches", s)[/color][/color][/color]
    <_sre.SRE_Mat ch object at 0x00B21800>

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

    SecondarySearch Term = '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

    Comment

    • Benji York

      #3
      Re: Matching zero only once using RE

      GregM wrote:[color=blue]
      > I am trying to see if a web page contains the exact text:
      > You have found 0 matches[/color]

      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

      Comment

      • Mike Meyer

        #4
        Re: Matching zero only once using RE

        "GregM" <gregm@taming-tech.com> writes:[color=blue]
        > 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[/color]

        Why in the gods names are you using an re for this? Just use in:
        [color=blue][color=green][color=darkred]
        >>> 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[/color][/color][/color]
        True[color=blue][color=green][color=darkred]
        >>> 'You have found 0 matches' in pretext2[/color][/color][/color]
        False[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

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

        <mike
        --
        Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

        Comment

        • GregM

          #5
          Re: Matching zero only once using RE

          Hi
          thanks to all of you. Mike I like your committee idea. where can I
          join? lol

          Greg.

          Comment

          • Aahz

            #6
            Re: Matching zero only once using RE

            In article <86ll15nri1.fsf @bhuda.mired.or g>, Mike Meyer <mwm@mired.or g> wrote:[color=blue]
            >
            >I think it's time to form a Committee for the Prevention of Regular
            >Expression Abuse.[/color]

            'Some people, when confronted with a problem, think "I know, I'll use
            regular expressions." Now they have two problems.' --Jamie Zawinski
            --
            Aahz (aahz@pythoncra ft.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

            Comment

            • Fredrik Lundh

              #7
              Re: Matching zero only once using RE

              Mike Meyer wrote:
              [color=blue]
              > I think it's time to form a Committee for the Prevention of Regular
              > Expression Abuse.[/color]

              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



              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>



              Comment

              • Tim Roberts

                #8
                Re: Matching zero only once using RE

                Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                >
                >I think it's time to form a Committee for the Prevention of Regular
                >Expression Abuse.[/color]

                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, timr@probo.com
                Providenza & Boekelheide, Inc.

                Comment

                Working...