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
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
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.
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
Copy link to clipboard
Copied
Wot he sed
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
Copy link to clipboard
Copied
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