Regular Expression Regex/Match fails if regular expression returns a null

T

tdmailbox

I have the following regular expression. It works fine if the regex
code returns a match. However if not the .match code fails.

How can I code this so that it skips the match if the regular
expression does not find anything?


Regex reg_unit_num = new Regex("L_unit_num.*?>(.*?)</td>",
RegexOptions.IgnoreCase);

string unit_num = reg_unit_num.Match(strHTMLPage).Result("$1");
 
M

Michael C#

Regex reg_unit_num = new Regex("L_unit_num.*?>(.*?)</td>",
RegexOptions.IgnoreCase);
Match m = reg_unit_num.Match(strHTMLPage);
string unit_num = "";
if (m.Success)
{
unit_num = m.Result("$1");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top