Newbie question: matching

Discussion in 'Python' started by josh R, Apr 16, 2004.

  1. josh R

    josh R Guest

    Hi all,

    I am trying to write some python to parse html code. I find it easier
    to do in perl, but would like to learn some basic python. My code
    looks like this:

    line = "<tr>eat at joe's</tr><tr>or else</tr><tr>you'll starve</tr>"
    so = re.compile("(\<tr\>.*?\<\\tr\>)")
    foo=so.match(line)
    print foo.groups()

    I'd like to get an array of elements that looks like this:

    array(0)= <tr>eat at joe's</tr>
    array(1)= <tr>or else</tr>
    array(2)= <tr>you'll starve</tr>

    Could you please tell me the correct way to do the matching?

    also, is there something similiar to perl's s/foo/bar/g?

    Thanks!!!
    Josh
    josh R, Apr 16, 2004
    #1
    1. Advertising

  2. josh R

    Tobiah Guest

    This should really be done with the XML parsing
    libraries. I don't remember the libs now, but
    I watched a co-worker translate HTML into XML,
    and then use minidom, or sax or some other lib
    to parse the XML. It is very convenient once
    you see how to do it. You either trigger an
    event for each tag/text, or get handed an entire
    object tree representing your HTML, which you can
    traverse and examine at a much higher level than
    you can trying to match tags with regular expressions.

    Toby

    josh R wrote:
    > Hi all,
    >
    > I am trying to write some python to parse html code. I find it easier
    > to do in perl, but would like to learn some basic python. My code
    > looks like this:
    >
    > line = "<tr>eat at joe's</tr><tr>or else</tr><tr>you'll starve</tr>"
    > so = re.compile("(\<tr\>.*?\<\\tr\>)")
    > foo=so.match(line)
    > print foo.groups()
    >
    > I'd like to get an array of elements that looks like this:
    >
    > array(0)= <tr>eat at joe's</tr>
    > array(1)= <tr>or else</tr>
    > array(2)= <tr>you'll starve</tr>
    >
    > Could you please tell me the correct way to do the matching?
    >
    > also, is there something similiar to perl's s/foo/bar/g?
    >
    > Thanks!!!
    > Josh
    Tobiah, Apr 16, 2004
    #2
    1. Advertising

  3. josh R wrote:

    >Hi all,
    >
    >I am trying to write some python to parse html code. I find it easier
    >to do in perl, but would like to learn some basic python. My code
    >looks like this:
    >
    >

    You will be doing yourself a disservice if you do this by hand. Check
    out http://www.diveintopython.org/ .. it's a free online book (also
    available in print). Jump to the chapter on XML parsing. You'll save
    yourself loads of time and effort, and you'll take advantage of the
    python libraries. After all, python *is* supposed to be "batteries
    included."
    Gabriel Cooper, Apr 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DelphiDude
    Replies:
    3
    Views:
    1,159
  2. Os Ai
    Replies:
    6
    Views:
    91
    Os Ai
    May 15, 2009
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  4. Vandana

    Perl Pattern Matching-- Newbie

    Vandana, Jan 19, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    97
    Tad McClellan
    Jan 19, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    212
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page