Solved

Regular Expression - replace pattern with another pattern

Posted on 2007-03-22
5
1,404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

717 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