How to extract string between """

StanONE

New member
Joined
Mar 29, 2004
Messages
4
Hi,
I want to write a Code-to-HTML libaray and add <font color="orange"> tags to string between "" (&quot; in html).

For example, the line :

"&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;"

will be replaced as :

"&lt;%@ TagName=<font color="orange">&quot;SourceCtrl&quot;</font> ABC=<font color="orange">&quot;&quot;6876786&quot;&quot;</font>%&gt;"

It means the line :

<%@ TageName="SourceCtrl" ABC=""6876786"">

would be:

<%@ TageName="SourceCtrl" ABC=""6876786"">

Iv tried to write an RE but failed.
c9c31a707704f73348609b52cfc5e1a6.gif


Const TAG_FNTORG As String = "<font color=" + Chr(34) + "orange" + Chr(34) + ">"

Dim sourceLine As String = "&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;"
Dim sourceLine2 As String = ""

searchExpr = "(?i)" + "(?<a>(&quot;)(.*?))" + "(?<b>(.+?))" + "(?<c>(&quot;))"
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
 
Last edited by a moderator:
Maybe

I may be making this too simple, but heres my suggestion:

To change

"&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;"

to

"&lt;%@ TagName=<font color="orange">&quot;SourceCtrl&quot;</font> ABC=<font color="orange">&quot;&quot;6876786&quot;&quot;</font>%&gt;"

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 Im off the mark, which Im reasonably sure that I am. :p
 
Umm, ouch, that hurt my head to think about!

mmmaybe:
Code:
((?:\&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:
Code:
(?<=\&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\;)[^\&]*\&*(?!quot\;))*
All text UpToAndIncluding the last ampersand(s), not including &quot;

(?:(?:\&*(?!quot\;)[^\&]*\&*(?!quot\;))*[^&]*)
All text UpToAndIncluding the last ampersand(s), not including &quot;,
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(?).
 
in RE: my last post,

the big problem i see is a string like:
&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;&quot;abc&quot;%&gt;

This will highlight like
&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;&quot;abc&quot;%&gt;
instead of
&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;&quot;abc&quot;%&gt;
Or
&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;&quot;abc&quot;%&gt;
 
seve7_wa said:
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.

Code:
Const TAG_FNTORG As String = "<font color=" + Chr(34) + "orange" + Chr(34) + ">"

Dim sourceLine As String = "&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;"
Dim sourceLine2 As String = ""

searchExpr = [color="#FF0080"]"(?i)" + "(?<a>(&quot;)(.*?))" + "(?<b>(.+?))" + "(?<c>(&quot;)+)"[/color]
replaceExpr = TAG_FNTORG + "${a}" + "${b}" + "${c}" + TAG_EFONT

If (Regex.IsMatch(sourceLine, searchExpr)) Then
sourceLine2 = Regex.Replace(sourceLine, searchExpr, replaceExpr)
End If
 
Code:
static void Main(string[] args)
{
	MatchEvaluator me = new MatchEvaluator(ReplaceWithOrnageFont);
	Regex regex = new Regex("(?<beginQuote>&quot;)(?<txt>.*?)(?<endQuote>&quot;)");

	string input = "&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;";
	Console.WriteLine("\"{0}\"\r\n", input);
	Console.WriteLine("\"{0}\"", regex.Replace(input, me));
}

static string ReplaceWithOrnageFont(Match match)
{
	return "<font color=\"orange\">&quot;" + match.Result("${txt}") + "&quot;</font>";
}


output said:
"&lt;%@ TagName=&quot;SourceCtrl&quot; ABC=&quot;&quot;6876786&quot;&quot;%&gt;"


"&lt;%@ TagName=<font color="orange">&quot;SourceCtrl&quot;</font> ABC=<fontcolor="orange">&quot;&quot;</font>6876786<font color="orange">&quot;&quot;</font>%&gt;"
 
Back
Top