Answered by:
Regular expressions and C# - replace pattern with pattern

Question
-
Sorry, my questions are not so difficult. But can't find answers how to work with Regular Expressions in C#.
What mean sign @ before blockquote in C# Regex?
And how to replace pattern with pattern? For example replace <img ...> with [img ....]
- Edited by Programmer SommerMVP Friday, December 9, 2011 10:10 AM
Friday, December 9, 2011 7:24 AM
Answers
-
> And how to replace pattern with pattern? For example replace <img ...> with [img ....]
var re = new Regex("<(?<tag>img.+?)>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); var str = re.Replace("text <img src='some'> text", "[${tag}]");
- Proposed as answer by Malobukv Friday, December 9, 2011 1:59 PM
- Marked as answer by Programmer SommerMVP Friday, December 9, 2011 2:52 PM
Friday, December 9, 2011 11:19 AM
All replies
-
Have found this:
string mes; mes = "<img>"; mes = Regex.Replace(mes, @"<img*>", delegate(Match match) { string v = match.ToString(); return "[" + v[1] + "]"; }); MessageBox.Show(mes);
but what to place in v[1]?Couse img tag can have some attributes inside...
Friday, December 9, 2011 10:13 AM -
Have found. May be will gonna use something like this:
string mes; mes = "<h1 style= >"; string res; res = Regex.Replace(mes, @"<h1.*>", delegate(Match match) { string v = match.ToString(); return "<" + v.Substring(4,v.Length-8)+">"; }); MessageBox.Show(res);
Friday, December 9, 2011 10:32 AM -
> And how to replace pattern with pattern? For example replace <img ...> with [img ....]
var re = new Regex("<(?<tag>img.+?)>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); var str = re.Replace("text <img src='some'> text", "[${tag}]");
- Proposed as answer by Malobukv Friday, December 9, 2011 1:59 PM
- Marked as answer by Programmer SommerMVP Friday, December 9, 2011 2:52 PM
Friday, December 9, 2011 11:19 AM -
Thank you!
And I have write such one:
messs = createHTML(messs, "<img[a-zA-z0-9\t\n .:;=\"/]*>");
and:string createHTML(string mes,string pattern){ string res; res = Regex.Replace(mes, @pattern, delegate(Match match) { string v = match.ToString(); return "<" + v.Substring(4, v.Length - 8) + ">"; },RegexOptions.IgnoreCase); return res; }
But now I thinking about how to check that string does not contained JavaScript?Add match inside delegate? Not a cute code....
Friday, December 9, 2011 12:50 PM -
> createHTML(messs, "<img[a-zA-z0-9\t\n .:;=\"/]*>"); [...] Add match inside delegate? ...
var html = createHTML("<img src='url' >\n<br/>"); ... string createHTML(string str) { MatchEvaluator me = m => string.Concat("<", m.Groups["tag"].Value, ">"); return Regex.Replace(str, "<(?<tag>.+?)(>|>)", me, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); }
- Edited by Malobukv Friday, December 9, 2011 1:51 PM
Friday, December 9, 2011 1:51 PM -
> But now I thinking about how to check that string does not contained JavaScript?
var re = new Regex(@"(<|<|&\#60;)script\b", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
- Proposed as answer by Malobukv Friday, December 9, 2011 1:59 PM
Friday, December 9, 2011 1:59 PM -
Yea, that's right. But I want to check this at the same regex where i searching for img.
I think that your first reply is the best. Gonna try it now...
Friday, December 9, 2011 2:30 PM -
Will mark your answer as answer. ))
But in code better will use my pattern. I have add here ? - couse there can be some chars after img or can be not
"<img[a-zA-z0-9\t\n -.:;=\"/]*?>"
Friday, December 9, 2011 2:52 PM -
> But I want to check this at the same regex where i searching for img.
could you please provide some of the possible texts?
but i think it would be better to work with standalone patterns.
and do search matches in parallel.
Friday, December 9, 2011 2:55 PM -
Sorry that so late.
Offcourse I can! ) It's simple easy:
"<img src="javascript:alert('css');">"
So, I want to replace when text match pattern "<(?<tag>.+?)(>|>)" and not contained "script" at the same time.
Have found conditional OR | but can't find AND... Anyway, thank you Malobukv you have helped! ( ty napisal ne tak uj i malo bukv ))
Monday, December 12, 2011 7:58 AM