StanONE Posted February 17, 2005 Posted February 17, 2005 (edited) Hi, I want to write a Code-to-HTML libaray and add <font color="orange"> tags to string between "" (" in html). For example, the line : "<%@ TagName="SourceCtrl" ABC=""6876786""%>" will be replaced as : "<%@ TagName=<font color="orange">"SourceCtrl"</font> ABC=<font color="orange">""6876786""</font>%>" It means the line : <%@ TageName="SourceCtrl" ABC=""6876786""> would be: <%@ TageName="SourceCtrl" ABC=""6876786""> I'v tried to write an RE but failed. http://www.xtremedotnettalk.com/x_images/images/smilies/frown.gif Const TAG_FNTORG As String = "<font color=" + Chr(34) + "orange" + Chr(34) + ">" Dim sourceLine As String = "<%@ TagName="SourceCtrl" ABC=""6876786""%>" Dim sourceLine2 As String = "" searchExpr = "(?i)" + "(?<a>(")(.*?))" + "(?<b>(.+?))" + "(?<c>("))" replaceExpr = TAG_FNTORG + "${a}" + "${b}" + "${c}" + TAG_EFONT If (Regex.IsMatch(sourceLine, searchExpr)) Then sourceLine2 = Regex.Replace(sourceLine, searchExpr, replaceExpr) End If Any correction appreciated! Regards, Stanley Edited February 21, 2005 by Iceplug Quote
Richard Crist Posted February 19, 2005 Posted February 19, 2005 Maybe I may be making this too simple, but here's my suggestion: To change "<%@ TagName="SourceCtrl" ABC=""6876786""%>" to "<%@ TagName=<font color="orange">"SourceCtrl"</font> ABC=<font color="orange">""6876786""</font>%>" use the following: Search for (TagName\=) replace with \1<font color="orange"> Search for ( ABC=) replace with </font>\1<font color="orange"> Search for (\%\>\;) replace with </font>\1 Please reply if I'm off the mark, which I'm reasonably sure that I am. :p Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
seve7_wa Posted February 24, 2005 Posted February 24, 2005 Umm, ouch, that hurt my head to think about! mmmaybe: ((?:\"\;\s*)+(?:(?:\&*(?!quot\;)[^\&]*\&*(?!quot\;))*[^&]*)(?:\"\;\s*)+) which should grab all text within the outermost group of quotes, and those outermost quotes, putting them in $parameter ... consumes these blocks so they are not included in future searches. This is what I was thinking, how I got there: (?<=\"\;)(\w+?)(?=\"\;) grabs [b]$parameter[/b] to let you wrap around the text inside the quotes. --since \w is not greedy it will be innermost, and it must be between quotes. But it will also grab text between two quoted blocks. :( \&(?!quot\;) Ampersands not including &qout; [^\&]*\&*(?!quot\;) All text UpToAndIncluding an ampersand(s), not including " (?:\&*(?!quot\;)[^\&]*\&*(?!quot\;))* All text UpToAndIncluding the last ampersand(s), not including " (?:(?:\&*(?!quot\;)[^\&]*\&*(?!quot\;))*[^&]*) All text UpToAndIncluding the last ampersand(s), not including ", and then all text until a quote. ((?:\"\;\s*)+(?:(?:\&*(?!quot\;)[^\&]*\&*(?!quot\;))*[^&]*)(?:\"\;\s*)+) grabs all text within the outermost group of quotes, and those outermost quotes ... consumes these blocks so they are not included in future searches. maybe not perfect, but close(?). Quote
seve7_wa Posted February 24, 2005 Posted February 24, 2005 in RE: my last post, the big problem i see is a string like: <%@ TagName="SourceCtrl" ABC=""6876786"""abc"%> This will highlight like <%@ TagName="SourceCtrl" ABC=""6876786"""abc"%> instead of <%@ TagName="SourceCtrl" ABC=""6876786"""abc"%> Or <%@ TagName="SourceCtrl" ABC=""6876786"""abc"%> Quote
StanONE Posted February 26, 2005 Author Posted February 26, 2005 in RE: my last post, the big problem i see is a string like: This will highlight like instead of Hi, Thanks for great help. Try this!But it met the same problem as yours. Const TAG_FNTORG As String = "<font color=" + Chr(34) + "orange" + Chr(34) + ">" Dim sourceLine As String = "<%@ TagName="SourceCtrl" ABC=""6876786""%>" Dim sourceLine2 As String = "" searchExpr = [color="#FF0080"]"(?i)" + "(?<a>(")(.*?))" + "(?<b>(.+?))" + "(?<c>(")+)"[/color] replaceExpr = TAG_FNTORG + "${a}" + "${b}" + "${c}" + TAG_EFONT If (Regex.IsMatch(sourceLine, searchExpr)) Then sourceLine2 = Regex.Replace(sourceLine, searchExpr, replaceExpr) End If Quote
HJB417 Posted February 26, 2005 Posted February 26, 2005 static void Main(string[] args) { MatchEvaluator me = new MatchEvaluator(ReplaceWithOrnageFont); Regex regex = new Regex("(?<beginQuote>")(?<txt>.*?)(?<endQuote>")"); string input = "<%@ TagName="SourceCtrl" ABC=""6876786""%>"; Console.WriteLine("\"{0}\"\r\n", input); Console.WriteLine("\"{0}\"", regex.Replace(input, me)); } static string ReplaceWithOrnageFont(Match match) { return "<font color=\"orange\">"" + match.Result("${txt}") + ""</font>"; } "<%@ TagName="SourceCtrl" ABC=""6876786""%>" "<%@ TagName=<font color="orange">"SourceCtrl"</font> ABC=<fontcolor="orange">""</font>6876786<font color="orange">""</font>%>" Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.