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

Optional word in regex

Guest
Feb 18, 2012 Feb 18, 2012

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!

TOPICS
Getting started
1.2K
Translate
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 18, 2012 Feb 18, 2012

Write two, and use or logic.

Translate
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
Guest
Feb 19, 2012 Feb 19, 2012
LATEST

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

Translate
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