Regular expression issue

D

dmbkiwi

I'm trying to parse a line of html as follows:

<td style="width:20%" align="left">101.120:( KPA (-)</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>

however, sometimes it looks like this:

<td style="width:20%" align="left">N/A</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>


I want to get either the numerical value 101.120 (which could be a
different number depending on the data that's been fed into the page,
or in terms of the second option, 'N/A'.

The regexp I'm using is:

..*?Pressure.*?"left">(?P<baro>\d+?|N/A)</td>|\sKPA.*?Snow\son\sGround

Can someone help me debug this. It's not picking up the number, and
I'm not sure I've got the syntax for '|' right, but can't find a
detailed tutorial on how to use |.

Any help would be appreciated.

Thanks

Matt
 
M

Marc 'BlackJack' Rintsch

I'm trying to parse a line of html as follows:

<td style="width:20%" align="left">101.120:( KPA (-)</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>

however, sometimes it looks like this:

<td style="width:20%" align="left">N/A</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>


I want to get either the numerical value 101.120 (which could be a
different number depending on the data that's been fed into the page,
or in terms of the second option, 'N/A'.

The regexp I'm using is:

.*?Pressure.*?"left">(?P<baro>\d+?|N/A)</td>|\sKPA.*?Snow\son\sGround

Can someone help me debug this. It's not picking up the number, and
I'm not sure I've got the syntax for '|' right, but can't find a
detailed tutorial on how to use |.

What about something like

align="left">((?P<baro>[\d.]+):\(\sKPA)|(?P<na>N/A).*Ground\)

You need the flags re.MULTILINE and re.DOTALL when compiling the regular
expression.

You'll have to check the 'baro' and 'na' groups to decide if it matched a
numerical value or 'N/A'.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Sibylle Koczian

I'm trying to parse a line of html as follows:

<td style="width:20%" align="left">101.120:( KPA (-)</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>

however, sometimes it looks like this:

<td style="width:20%" align="left">N/A</td>
<td style="width:35%" align="left">Snow on Ground)0 </td>


I want to get either the numerical value 101.120 (which could be a
different number depending on the data that's been fed into the page,
or in terms of the second option, 'N/A'.

The regexp I'm using is:

.*?Pressure.*?"left">(?P<baro>\d+?|N/A)</td>|\sKPA.*?Snow\son\sGround

Wouldn't it be simpler to use HTMLParser or something similar first to
separate text and HTML tags and get the content of each cell separately?
Then you have only to find the 'right' cell, possibly quite simply by
its position in the HTML table, and check if it contains 'N/A' or
something numeric (that check wouldn't need a regular expression if its
really so simple).

No Python here so I can't try it out to be more specific, but look for
HTMLParser in the library reference.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top