Skip to main content
February 12, 2011
Answered

Need help with regex

  • February 12, 2011
  • 1 reply
  • 711 views

Hi,

I am very new to using regular expression, and I have a problem I can't figure out myself.

The problem is how do I write a reFindNoCase function that will pull up the value inside a <td> tag:

1:  <td>100</td>

2:  <td><span class="some_class"> -100 </span></td>

So, inside the <td></td> tag there is an optional <span> tag.  This tag is to be ignored, such that  (1) should give me 100 while (2) should give me -100.

I have played around with different regex strings, but I just can't figure it out.  Can someone help?

Thanks!

ML

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Just a tip: if you want to group something for the purposes of applying a modifier to it, but you do not need to capture it for a back reference or a capture group, use the ?: modifier in the grouping, eg:

    <td>(?:<span(?:.*?)>)?(.*?)(?:</span>)?</td>

    That said, in this case one probably doesn't need the parentheses at all for the bit within the span, eg:

    <td>(?:<span.*?>)?(.*?)(?:</span>)?</td>

    --

    Adam

    1 reply

    Owainnorth
    Inspiring
    February 12, 2011

    Probably not the neatest, but works:

    <td>(<span(.*?)>)?(.*?)(</span>)?</td>


    <td>               -- guaranteed string
      (<span(.*?)>)    -- a span tag with any characters inside, until a closing tag
                   ?   -- the entire span tag is optional
      (.*?)            -- then the minimum number of characters until either:
      (</span>)?       -- an optional closing span tag
    </td>              -- or the closing td

    Group 3 should give you the value you need.

    HTH.

    Adam Cameron.Correct answer
    Inspiring
    February 12, 2011

    Just a tip: if you want to group something for the purposes of applying a modifier to it, but you do not need to capture it for a back reference or a capture group, use the ?: modifier in the grouping, eg:

    <td>(?:<span(?:.*?)>)?(.*?)(?:</span>)?</td>

    That said, in this case one probably doesn't need the parentheses at all for the bit within the span, eg:

    <td>(?:<span.*?>)?(.*?)(?:</span>)?</td>

    --

    Adam

    Owainnorth
    Inspiring
    February 12, 2011

    Wot he sed