Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Iceplug
Posted

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

 

(\%\&gt\;)

 

replace with

 

</font>\1

 

Please reply if I'm off the mark, which I'm reasonably sure that I am. :p

Posted

Umm, ouch, that hurt my head to think about!

 

mmmaybe:

((?:\&quot\;\s*)+(?:(?:\&*(?!quot\;)[^\&]*\&*(?!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:

(?<=\&quot\;)(\w+?)(?=\&quot\;)
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.

((?:\&quot\;\s*)+(?:(?:\&*(?!quot\;)[^\&]*\&*(?!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(?).

Posted

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"%>

Posted
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 

Posted

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>%>"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...