Copy link to clipboard
Copied
Hi,
I need some help in doing a regex find. I want to find the content of a <td> tag. However, the content might be enclosed by <span> tag.
<td>abc</td>
<td><span>abc</span></td>
How do I write the regex that can capture abc in either situation?
Thank you!
Copy link to clipboard
Copied
Write two, and use or logic.
Copy link to clipboard
Copied
Hi Dan,
Do you mean using two regex string like this:
regexStr1 = "<td><span>(.*?)</span></td>";
regexStr2 = "<td>(.*?)</td>";
And then run them using refindnocase()?
result1 = reFindNoCase( regexStr1, str, 1, "yes" );
result2 = reFindNoCase( regexStr2, str, 1, "yes" );
if( result1.pos[1] )
targetStr = mid( str, result1.pos[ 2 ], result1.len[ 2 ] );
else if ( result2.pos[1] )
targetStr = mid( str, result2.pos[ 2 ], result2.len[ 2 ] );
else
throw "Target string not found";
I also read that you can set an optional group in regex without triggering it as a back reference. It works by enclosing the optional group with (?: and )?. So I tried:
regexStr3 = "<td>(?:\<span\>)?(.*?)(?:\</span\>)?</td>"
When I tried this last week, I couldn't get it to work, but now it seems to work fine. Not sure what I did wrong.
Thanks!
ML