Link to home
Start Free TrialLog in
Avatar of ignatiusst
ignatiusst

asked on

Regular Expression - replace pattern with another pattern

I am working in C#, and I am trying to replace the brackets on an html tag.  For example, I want to change <ul type='square'> to &lt;ul type='square'&gt;

With the Regex.Replace, I can find the pattern (<ul(\s+type\s*=\s*(?:""(disc|circle|square)""|'(disc|circle|square)'|(disc|circle|square)))?\s*>), but is there any way to replace with another patter (eg: &ltul(\s+type\s*=\s*(?:""(disc|circle|square)""|'(disc|circle|square)'|(disc|circle|square)))?\s*&gt;)?
ASKER CERTIFIED SOLUTION
Avatar of exoska
exoska

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of amit_g
Change the RegExp to

<(ul(\s+type\s*=\s*(?:""(disc|circle|square)""|'(disc|circle|square)'|(disc|circle|square)))?\s*)>

and use replacement string

&lt;$1&gt;
I am curious ... is there something wrong with using HTTPutility.HtmlEncode which will do just this?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpServerUtilityClassHtmlEncodeTopic.asp
Avatar of ignatiusst
ignatiusst

ASKER

exoska - thanks!  sorry it took so long to get back... I just copied the replacement string you provided (&lt;$1&gt;), and it took me a bit of trouble-shooting to realize you had also enclosed everything inside the tags in parenthesis...

amit_g - two minutes earlier, and...  :)

gregoryyoung - thanks for the link!  This might be what I am looking for (I do like trying to figure these things out on my own before I use a packaged solution.. call me sick... :) ), though I am only interested in a sub-set of valid HTML - if I receive (for example) <table>, I want to leave it as-is...  I will have to investigate whether the HtmlEncode can handle this.
oh i m glad :)