Regular expression issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dmbkiwi@gmail.com

    #1

    Regular expression issue

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

    <td style="width:20 %" align="left">10 1.120:( KPA (-)</td>
    <td style="width:35 %" align="left">Sn ow 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">Sn ow 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.*?Sno w\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

  • Marc 'BlackJack' Rintsch

    #2
    Re: Regular expression issue

    In <1153304898.226 689.254330@m79g 2000cwm.googleg roups.com>, dmbkiwi wrote:
    I'm trying to parse a line of html as follows:
    >
    <td style="width:20 %" align="left">10 1.120:( KPA (-)</td>
    <td style="width:35 %" align="left">Sn ow 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">Sn ow 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.*?Sno w\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

    Comment

    • Sibylle Koczian

      #3
      Re: Regular expression issue

      dmbkiwi@gmail.c om schrieb:
      I'm trying to parse a line of html as follows:
      >
      <td style="width:20 %" align="left">10 1.120:( KPA (-)</td>
      <td style="width:35 %" align="left">Sn ow 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">Sn ow 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.*?Sno w\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.

      --
      Dr. Sibylle Koczian
      Universitaetsbi bliothek, Abt. Naturwiss.
      D-86135 Augsburg
      e-mail : Sibylle.Koczian @Bibliothek.Uni-Augsburg.DE

      Comment

      Working...