0

I'm working on a small assignment that requires the use of regular expressions with HTML strings. My current problem is properly obtaining strings enclosed within HTML tags.

For instance:

I have a string

<p>&lt;Placeholder&gt;</p>

I've been able to obtain the contents with the following regex

private string Unescape(){
    string s = WebUtility.HtmlDecode("<p>&lt;Placeholder&gt;</p>");
    string dec = Regex.Replace(s, "^<.*?>|^<.*?><.*?>", "");
    return Regex.Replace(dec, "</.*?>$|</.*?></.*?>$", "");
}

Which would return:

<Placeholder>

However, should the string contain an additional HTML tag, e.g.:

<p><strong>Placeholder</strong></p>

I would get this

<strong>Placeholder 

It appears I'm only able to successfully remove the closing tag(s), but I can't do the same with the opening tag(s). Could anybody tell me where I've gone wrong?

EDIT:

To summarize, is there a way for me to treat the string enclosed within HTML tags as literal? To cover the possibility that the string could contain special characters (e.g. > <)

7
  • 1
    Try using HtmlAgilityPack http://htmlagilitypack.codeplex.com/ Commented Oct 9, 2012 at 6:59
  • 4
    stackoverflow.com/questions/1732348/… Commented Oct 9, 2012 at 6:59
  • I'm trying to avoid libraries if possible. But I'll check it out! Commented Oct 9, 2012 at 7:07
  • In my own project, I included the necessary source code, there's about 15 files. So it's pretty compact ;) I believe regex is an overkill solution. Good luck anyway! Commented Oct 9, 2012 at 7:09
  • "I'm trying to avoid libraries if possible." Reinventing the wheel is not good. You should use libraries if possible, unless you have a really good reason not to. Commented Oct 9, 2012 at 7:29

1 Answer 1

1

I am not sure if your will get happy with your regex usage on html, but I want to explain what the problem for your "mis"match is:

An alternation will use the first match it will find and will not look for further matches. So when you search at the start for

^<.*?>|^<.*?><.*?>

on the string

<p><strong>Placeholder</strong></p>

It will match on the first alternative and therefore it will end with a successful match on the first alternative. So if you want to match <p><strong> at the start you should change the ordering in the alternation. but only for the part at the start of the string, for the end of the string your ordering is fine.

So for your example this would work:

private string Unescape(){
    string s = WebUtility.HtmlDecode("<p>&lt;Placeholder&gt;</p>");
    string dec = Regex.Replace(s, "^<.*?><.*?>|^<.*?>", "");
    return Regex.Replace(dec, "</.*?>$|</.*?></.*?>$", "");
}

==> The ordering inside an alternation can be important

An alternative would be to use a quantifier instead of an alternation:

string dec = Regex.Replace(s, "^(?:<.*?>)+", "");
return Regex.Replace(dec, "(?:</.*?>)+$", "");

this would work also for more than 2 tags.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.