Solved

Regular Expression - replace pattern with another pattern

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
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!
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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