How to replace it using Regular expression in .Net
-
Friday, February 10, 2012 7:53 AM
Hi,
Whats the regular expression in .Net to replace
<p style=\"BACKGROUND: white\" />
with
<p style=\"BACKGROUND: white\"></p>
Thanks,
Manvir
Manvir
All Replies
-
Friday, February 10, 2012 10:28 AM
If it is this specific tag you want to replace, you can simply use string.Replace, no need for regular expressions what so ever.
If you need to replace multiple variations of this tag, please share with us what kind of variations you're expecting to need to replace. Though if there's just a few more variations, it'll probably be better to handle these kinds of things through a DOM parser such as the HTML Agility Pack.
My blog: blog.jessehouwing.nl
-
Sunday, February 12, 2012 10:46 PM
Hi Jesse,
Thanks for the reply. Actually, below expression
<p style=\"BACKGROUND: white\" />
is returned by HTMLAgility pack and i want it to convert to
<p style=\"BACKGROUND: white\"></p>.
There are multiple variations of this tag and like "style" this tag can have any possible attribute.
Thanks,
Manvir
Manvir
-
Wednesday, March 14, 2012 8:13 PM
Using Linq to sql might be an option:
[TestMethod] public void LinqTest() { string test = "<p style=\"BACKGROUND: white\" />"; XElement xElement = XElement.Parse(test); xElement.SetValue(""); string s = xElement.ToString(); }
Trying to learn
- Edited by Johan20D Wednesday, March 14, 2012 11:08 PM
-
Tuesday, March 20, 2012 2:16 AM
Hi you can try this as follows:
string pattern = @"(<p\s(?>[^/]*))/>"; Regex r = new Regex(pattern); string input = "<p style=\"BACKGROUND: white\" />"; string temp = r.Replace(input, "$1><p>"); Console.WriteLine(temp);Please Mark it as answer, if it helps solve your problem.
-
Thursday, April 12, 2012 9:55 AM
HI,
Phape solution should work.
HIs RegX is fulfilling the requirements.
-
Monday, April 23, 2012 2:39 AM
Slightly improved, untested.
string pattern = @"<([a-zA-Z]+)(\s(?>[^/]*))/>";
string temp = r.Replace(input, "<$1$2><$1>");
Using two groups allows us to pull out the tag name (first group alpha before spaces). This however would expand img tags which should not be. You could however list a number of tags with bars between, eg (p|div) to match literal text. You can also make it case insensitive though options which makes it easier (eg p and P would be matched with a simple 'p' not having to code [pP].
Ta Ken
- Proposed As Answer by Romulus C Friday, September 07, 2012 8:41 AM
-
Friday, September 07, 2012 8:34 AM
Slightly improved, untested.
string pattern = @"<([a-zA-Z]+)(\s(?>[^/]*))/>";
string temp = r.Replace(input, "<$1$2><$1>");
Using two groups allows us to pull out the tag name (first group alpha before spaces). This however would expand img tags which should not be. You could however list a number of tags with bars between, eg (p|div) to match literal text. You can also make it case insensitive though options which makes it easier (eg p and P would be matched with a simple 'p' not having to code [pP].
Ta Ken
Waratah's answer is the solution with a small change in order to remove spaces before ">" and properly add the end "</p>" tag:
string pattern = @"<([a-zA-Z]+)(\s(?>[^/]*?))\s*/>"; string temp = r.Replace(input, "<$1$2></$1>");
Please note that this refortmats all of your matching tags in the input string. For matching only the paragraph tags this must be changed to:
string pattern = @"<p(\s(?>[^/]*?))\s*/>"; string temp = r.Replace(input, "<p$1></p>");

