Solved

Regular Expression - replace pattern with another pattern

Posted on 2007-03-22
5
1,395 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now