Connecting Tech Pros Worldwide Forums | Help | Site Map

Replacing Text without changing case??

wildman@noclient.net
Guest
 
Posts: n/a
#1: Mar 6 '08
RE: Replacing Text without changing case??

This code works great, but case has to be exact.

Research.Text = Research.Text.Replace(textboxSearch.Text, "<b>" +
textboxSearch.Text + "</b>")



I found some c# code, but I can't make it work in vb.net:

Regex.Replace(Research.Text, textboxSearch.Text, New
MatchEvaluator(HiLite), RegexOptions.IgnoreCase)


....
Public Shared Function HiLite(ByVal match As Match) As String
Return "<b>" + match.Value + "</b>*"
End Function


Its complaining about Hilite in the first line.



wildman@noclient.net
Guest
 
Posts: n/a
#2: Mar 7 '08

re: Replacing Text without changing case??


THANK YOU!

Quote:
Regex.Replace(string.format("({0})", Regex.Escape(input)), "<b>$1</b>", RegexOptions.IgnoreCase)
Presuming

user search input : textboxSearch.text
replace is : "<b>"+textboxSearch.text+"</b>"
data I'm looking to impact is : Research.Text (a gridview row label
control)


How would I use the above?

I tried:

Imports System.Text.RegularExpressions

Research.Text = Regex.Replace(String.Format("({0})",
Regex.Escape(textboxSearch.Text)), "<b>$1</b>",
RegexOptions.IgnoreCase)


Getting an error on Regex.Replace : Reference to a non-shared member
requires an object reference.

So I noticed it appears to be formated incorrectly and probably should
be :

Research.Text = Regex.Replace(String.Format("({0})"),
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)

However that did not work.

I also tried:


No effect:
Research.Text = Regex.Replace(String.Format("({0})"),
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)


replaces string with actually "$1":
'Research.Text = Regex.Replace(Research.Text,
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)


works, in finding the strings, but replaces with wrong case:
'Research.Text = Regex.Replace(Research.Text,
Regex.Escape(textboxSearch.Text), "<b>"+ textboxSearch.Text+"</b>",
RegexOptions.IgnoreCase)



Thanks again.




Jesse Houwing
Guest
 
Posts: n/a
#3: Mar 7 '08

re: Replacing Text without changing case??


Hello wildman@noclient.net,
Quote:
THANK YOU!
>
Quote:
>Regex.Replace(string.format("({0})", Regex.Escape(input)),
>"<b>$1</b>", RegexOptions.IgnoreCase)
>>
Quote:
Presuming
>
user search input : textboxSearch.text
replace is : "<b>"+textboxSearch.text+"</b>"
data I'm looking to impact is : Research.Text (a gridview row label
control)
How would I use the above?
>
I tried:
>
Imports System.Text.RegularExpressions
>
Research.Text = Regex.Replace(String.Format("({0})",
Regex.Escape(textboxSearch.Text)), "<b>$1</b>",
RegexOptions.IgnoreCase)
>
Getting an error on Regex.Replace : Reference to a non-shared member
requires an object reference.
>
So I noticed it appears to be formated incorrectly and probably should
be :
>
Research.Text = Regex.Replace(String.Format("({0})"),
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)
>
However that did not work.
>
I also tried:
>
No effect:
Research.Text = Regex.Replace(String.Format("({0})"),
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)
replaces string with actually "$1":
'Research.Text = Regex.Replace(Research.Text,
Regex.Escape(textboxSearch.Text), "<b>$1</b>",
RegexOptions.IgnoreCase)
works, in finding the strings, but replaces with wrong case:
'Research.Text = Regex.Replace(Research.Text,
Regex.Escape(textboxSearch.Text), "<b>"+ textboxSearch.Text+"</b>",
RegexOptions.IgnoreCase)
Sorry, I mixed the input and the pattern parameters to the Regex.Replace
function.

This should do it (in C#, I'm no star in VB):

string input = "a.b.c.d.e.f.g.h.i.j";
string output = Regex.Replace(input, string.Format("({0})", Regex.Escape(input)),
"<b>$1</b>");
Console.WriteLine(output);

--
Jesse Houwing
jesse.houwing at sogeti.nl


wildman@noclient.net
Guest
 
Posts: n/a
#4: Mar 12 '08

re: Replacing Text without changing case??


I'm close, but this ends up highighting all of research.text when a
match is found. How can I highlight just the found string in it's
original case?

Research.Text = Regex.Replace(Research.Text, String.Format("({0})",
Regex.Escape(Research.Text)), "<FONT style='background-color:Yellow'>
$1</FONT>")

thanks.
Jesse Houwing
Guest
 
Posts: n/a
#5: Mar 14 '08

re: Replacing Text without changing case??


Hello wildman@noclient.net,
Quote:
I'm close, but this ends up highighting all of research.text when a
match is found. How can I highlight just the found string in it's
original case?
>
Research.Text = Regex.Replace(Research.Text, String.Format("({0})",
Regex.Escape(Research.Text)), "<FONT style='background-color:Yellow'>
$1</FONT>")
That shouldn't be too hard, but currently you're puttign the whole string
in for both input and pattern.
Quote:
Research.Text = Regex.Replace(
Research.Text, <== input
String.Format("({0})", Regex.Escape(Research.Text)), <== text to search
"<FONT style='background-color:Yellow'>$1</FONT>" <== replacement
)

So make sure you only put the text to search in the correct place.

--
Jesse Houwing
jesse.houwing at sogeti.nl


Closed Thread