By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

Replacing Text without changing case??

P: n/a
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.
Mar 6 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
THANK YOU!

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.


Mar 7 '08 #2

P: n/a
Hello wi*****@noclient.net,
THANK YOU!
>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)
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
Mar 7 '08 #3

P: n/a
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.
Mar 12 '08 #4

P: n/a
Hello wi*****@noclient.net,
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.
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
Mar 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.