• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help with regex

Explorer ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

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

Views

586

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 12, 2011 Feb 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

Votes

Translate

Translate
Guide ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

Wot he sed

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

Adam Cameron. wrote:

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:

Adam, I didn't know that tip and it turned out to be the difference.  Thank you so much!

MCL

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2011 Feb 12, 2011

Copy link to clipboard

Copied

LATEST

Cool. Good to see that random posts made whilst at the pub waiting for Eng v Italy to start (and mates to arrive to watch same) aren't such a bad idea.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation