Regular Expression help

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

    #1

    Regular Expression help

    I have some data and I need to put it in a list in a particular way. I
    have that figured out but there is " stuff " in the data that I don't
    want.

    Example:

    10:00am - 11:00am:</b> <a
    href="/tvpdb?d=tvp&id= 167540528&cf=0& lineup=us_KS578 36d&channels=us _KCTV&chspid=16 6030466&chname= CBS&progutn=114 6150000&.intl=u s">The
    Price Is Right</a><em>

    All I want is " Price Is Right "

    Here is the re.

    findshows =
    re.compile(r'(\ d\d:\d\d\D\D\s-\s\d\d:\d\d\D\D :*.*</a><em>)')

    I have used a for loop to remove the extra data but then it ruins the
    list that I am building. Basically I want the list to be something
    like this.

    [[Government Access], [Price Is Right, Guiding Light, Another show]]

    the for loop just comma deliminates all of them so I lose the list in a
    list that I need. I hope I have explained this well enough. Any help
    or ideas would be appreciated.

    TIA

  • Edward Elliott

    #2
    Re: Regular Expression help

    RunLevelZero wrote:[color=blue]
    > 10:00am - 11:00am:</b> <a href="/tvpdb?d=tvp&id= 167540528&[snip]>The
    > Price Is Right</a><em>
    >
    > All I want is " Price Is Right "
    >
    > Here is the re.
    >
    > findshows =
    > re.compile(r'(\ d\d:\d\d\D\D\s-\s\d\d:\d\d\D\D :*.*</a><em>)')[/color]

    1. A regex remembers everything it matches -- no need to wrap the entire
    thing in parens. Just call group() on the returned MatchObject.

    2. If all you want is the link text, you don't need to do so much matching.
    If you don't need the time, don't match it in the first place. If you're
    using it as a marker, try matching each time with r'[\d:]{4,5}[ap]m'. Not
    as exact but a bit simpler. Or just r'[\d:apm]{6,7}'

    3. To grab what's inside the link: r'<a[^>]*>(.*?)</a>'

    4. If the link text itself contains html tags, you'll have to strip those
    off separately. Extracting the text from arbitrarily nested html tags in
    one shot requires a parser, not a regex.

    5. If you're just going to run this regex repeatedly on an html doc and make
    a list of the results, it's easier to read the whole doc into a string and
    then use re.findall.

    [color=blue]
    > I have used a for loop to remove the extra data but then it ruins the
    > list that I am building. Basically I want the list to be something
    > like this.
    >
    > [[Government Access], [Price Is Right, Guiding Light, Another show]]
    >
    > the for loop just comma deliminates all of them so I lose the list in a
    > list that I need. I hope I have explained this well enough. Any help
    > or ideas would be appreciated.[/color]

    No one can help with that unless you show us how you're building your list.


    Comment

    • RunLevelZero

      #3
      Re: Regular Expression help

      Great I will test this out once I have the time... thanks for the quick
      response

      Comment

      • johnzenger@gmail.com

        #4
        Re: Regular Expression help

        If you are parsing HTML, it may make more sense to use a package
        designed especially for that purpose, like Beautiful Soup.

        Comment

        • RunLevelZero

          #5
          Re: Regular Expression help

          I considered that but what I need is simple and I don't want to use
          another library for something so simple but thank you. Plus I don't
          understand them all that well :)

          Comment

          • johnzenger@gmail.com

            #6
            Re: Regular Expression help

            If what you need is "simple," regular expressions are almost never the
            answer. And how simple can it be if you are posting here? :)

            BeautifulSoup isn't all that hard. Observe:
            [color=blue][color=green][color=darkred]
            >>> from BeautifulSoup import BeautifulSoup
            >>> html = '10:00am - 11:00am:</b> <a href="/tvpdb?d=tvp&id= 167540528&[snip]>The Price Is Right</a><em>'
            >>> soup = BeautifulSoup(h tml)
            >>> soup('a')[/color][/color][/color]
            [<a href=""/tvpdb?d=tvp&id= 167540528&">The Price Is Right</a>][color=blue][color=green][color=darkred]
            >>> for show in soup('a'):[/color][/color][/color]
            print show.contents[0]


            The Price Is Right



            RunLevelZero wrote:[color=blue]
            > I considered that but what I need is simple and I don't want to use
            > another library for something so simple but thank you. Plus I don't
            > understand them all that well :)[/color]

            Comment

            • RunLevelZero

              #7
              Re: Regular Expression help

              r'<a[^>]*>(.*?)</a>'

              With a slight modification that did exactly what I wanted, and yes the
              findall was the only way to get all that I needed as I buffered all the
              read.

              Thanks a bunch.

              Comment

              • RunLevelZero

                #8
                Re: Regular Expression help

                Interesting... thank you.

                Comment

                • Edward Elliott

                  #9
                  Re: Regular Expression help

                  johnzenger@gmai l.com wrote:[color=blue]
                  > If you are parsing HTML, it may make more sense to use a package
                  > designed especially for that purpose, like Beautiful Soup.[/color]

                  I don't know Beautiful Soup, but one advantage regexes have over some
                  parsers is handling malformed html. Omitted closing tags can wreak havoc.
                  Regexes can also help if you only want elements preceded/followed by a
                  certain sibling or cousin in the parse tree. It all depends on what you're
                  trying to accomplish. In general though, yes parsers are better suited to
                  extracting from markup.

                  Comment

                  • John Bokma

                    #10
                    Re: Regular Expression help

                    Edward Elliott <nobody@127.0.0 .1> wrote:
                    [color=blue]
                    > johnzenger@gmai l.com wrote:[color=green]
                    >> If you are parsing HTML, it may make more sense to use a package
                    >> designed especially for that purpose, like Beautiful Soup.[/color]
                    >
                    > I don't know Beautiful Soup, but one advantage regexes have over some
                    > parsers is handling malformed html. Omitted closing tags can wreak
                    > havoc. Regexes can also help if you only want elements
                    > preceded/followed by a certain sibling or cousin in the parse tree.
                    > It all depends on what you're trying to accomplish. In general
                    > though, yes parsers are better suited to extracting from markup.[/color]

                    A parser can be written in such a way that it doesn't give up on malformed
                    HTML. Probably less hard then coming up with regexes that handle HTML
                    that's well-formed. (and that coming from a Perl programmer ;-) )

                    --
                    John MexIT: http://johnbokma.com/mexit/
                    personal page: http://johnbokma.com/
                    Experienced programmer available: http://castleamber.com/
                    Happy Customers: http://castleamber.com/testimonials.html

                    Comment

                    • Kent Johnson

                      #11
                      Re: Regular Expression help

                      Edward Elliott wrote:[color=blue]
                      > johnzenger@gmai l.com wrote:[color=green]
                      >> If you are parsing HTML, it may make more sense to use a package
                      >> designed especially for that purpose, like Beautiful Soup.[/color]
                      >
                      > I don't know Beautiful Soup, but one advantage regexes have over some
                      > parsers is handling malformed html.[/color]

                      Beautiful Soup is intended to handle malformed HTML and seems to do
                      pretty well.

                      Kent

                      Comment

                      Working...