Where can be a problem?

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

    #1

    Where can be a problem?

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

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

  • Peter Otten

    #2
    Re: Where can be a problem?

    Lad wrote:
    [color=blue]
    > I use the following
    > ###############
    > import re
    > Results=[]
    > data1='<a href="detailasp xmember=15015&m ode=advert" </a><a
    > href="detailasp xmember=15016&m ode=advert" </a><a
    > href="detailasp xmember=15017&m ode=advert" </a>'
    > ID = re.compile(r'^. *=(\d+)&.*$',re .MULTILINE)
    > Results=re.find all(ID,data1)
    > print Results
    > #############
    > to extract from data1 all numbers such as 15015,15016,150 17
    >
    > But the program extracts only the last number 15017.
    > Why?
    > Thank you for help
    > La.[/color]

    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

    Comment

    • Lad

      #3
      Re: Where can be a problem?

      Peter,
      I tried exactly this
      ########
      import re
      Results=[]
      data1='<a href="detailasp xmember=15015&m ode=advert" </a><a
      href="detailasp xmember=15016&m ode=advert" </a><a
      href="detailasp xmember=15017&m ode=advert" </a>'
      ID = re.compile(r'^. *=(\d+)&.*$',re .MULTILINE)
      Results=re.find all(ID,data1)
      print "Results are= ",Results
      #########
      and received
      Results are= ['15017']

      Not all numbers

      What exactly did you get?
      Thanks.
      L.

      Comment

      • Peter Otten

        #4
        Re: Where can be a problem?

        Lad wrote:
        [color=blue]
        > Peter,
        > I tried exactly this
        > ########
        > import re
        > Results=[]
        > data1='<a href="detailasp xmember=15015&m ode=advert" </a><a
        > href="detailasp xmember=15016&m ode=advert" </a><a
        > href="detailasp xmember=15017&m ode=advert" </a>'
        > ID = re.compile(r'^. *=(\d+)&.*$',re .MULTILINE)
        > Results=re.find all(ID,data1)
        > print "Results are= ",Results
        > #########
        > and received
        > Results are= ['15017']
        >
        > Not all numbers
        >
        > What exactly did you get?[/color]

        With /exactly/ this, I get:

        $ cat lad1.py
        import re
        Results=[]
        data1='<a href="detailasp xmember=15015&m ode=advert" </a><a
        href="detailasp xmember=15016&m ode=advert" </a><a
        href="detailasp xmember=15017&m ode=advert" </a>'
        ID = re.compile(r'^. *=(\d+)&.*$',re .MULTILINE)
        Results=re.find all(ID,data1)
        print "Results are= ",Results
        $ python lad1.py
        File "lad1.py", line 3
        data1='<a href="detailasp xmember=15015&m ode=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="detailasp xmember=15015&m ode=advert" </a><a
        href="detailasp xmember=15016&m ode=advert" </a><a
        href="detailasp xmember=15017&m ode=advert" </a>'''
        ID = re.compile(r'^. *=(\d+)&.*$',re .MULTILINE)
        Results=re.find all(ID,data1)
        print "Results are= ",Results
        $ python lad2.py
        Results are= ['15015', '15016', '15017']

        Peter

        Comment

        • Lad

          #5
          Re: Where can be a problem?

          Thank you Peter for help.
          The reason why it did not work was the fact that findall function
          required CRLF among lines

          Comment

          • Paul McGuire

            #6
            Re: Where can be a problem?

            Try this, its a bit more readable than your re.

            from pyparsing import Word,nums,Liter al,replaceWith

            data1='''<a href="detailasp xmember=15015&m-ode=advert" </a><a
            href="detailasp xmember=15016&m ­ode=advert" </a><a
            href="detailasp xmember=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.scan String(data1) ]

            # just for grins, here is how to convert the numbers to the
            # string "###"
            number.setParse Action( replaceWith("## #") )
            print number.transfor mString(data1)


            Prints:

            ['15015', '15016', '15017']
            <a href="detailasp xmember=###&m-ode=advert" </a><a
            href="detailasp xmember=###&m­o de=advert" </a><a
            href="detailasp xmember=###&m­o de=advert" </a>

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

            -- Paul

            Comment

            Working...