Replacing Text without changing case??

W

wildman

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.
 
J

Jesse Houwing

Hello (e-mail address removed),
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.

This isn't that hard to do. You don't even need a Match evaluator.

Use

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

This will copy the value you found in the first group (which is created by
adding (...) around the text to match), with <b>valueofthatgroup</b>. This
accomplishes your goal.

There is one thing you have to take care of though. As you're using the users
input you are now allowing the user to insert additional regex into the input.
The Regex class has a static method called Escape to take care of that, to
you'll get:

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

Jesse Houwing

Hello (e-mail address removed),
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.


Ohh and to fix the problem you're having with the current implementation
(which is slower and still needs the Regex.Escape) you probably have to do
either:
 
W

wildman

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.
 
J

Jesse Houwing

Hello (e-mail address removed),
THANK YOU!
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);
 
W

wildman

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.
 
J

Jesse Houwing

Hello (e-mail address removed),
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top