Solved

Regular Expression - replace pattern with another pattern

Posted on 2007-03-22
5
1,399 Views
Last Modified: 2008-01-09
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;)?
0
Comment
Question by:ignatiusst
5 Comments
 
LVL 3

Accepted Solution

by:
exoska earned 250 total points
ID: 18775739
well
try this;


string ResultString = null;
try {
      ResultString = Regex.Replace(SubjectString, "<(ul\\s+type\\s*=\\s*(?:\"\"(disc|circle|square)\"\"|'(disc|circle|square)'|(disc|circle|square))?\\s*)>", "&lt$1&gt;");
} catch (ArgumentException ex) {
      // Syntax error in the regular expression
}

0
 
LVL 58

Expert Comment

by:amit_g
ID: 18775752
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;
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 18776078
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
0
 

Author Comment

by:ignatiusst
ID: 18776447
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.
0
 
LVL 3

Expert Comment

by:exoska
ID: 18776521
oh i m glad :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question