Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Replacing Text without changing case??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wildman@noclient.net - 06 Mar 2008 21:46 GMT
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.
Jesse Houwing - 06 Mar 2008 22:00 GMT
Hello wildman@noclient.net,

> RE: Replacing Text without changing case??
>
[quoted text clipped - 13 lines]
> 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)

--
Jesse Houwing
jesse.houwing at sogeti.n
wildman@noclient.net - 07 Mar 2008 10:19 GMT
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.
Jesse Houwing - 07 Mar 2008 14:36 GMT
Hello wildman@noclient.net,

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

> Presuming
>
[quoted text clipped - 38 lines]
> 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 - 12 Mar 2008 21:05 GMT
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 - 14 Mar 2008 19:38 GMT
Hello wildman@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
[quoted text clipped - 3 lines]
> 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
Jesse Houwing - 06 Mar 2008 22:04 GMT
Hello wildman@noclient.net,

> RE: Replacing Text without changing case??
>
[quoted text clipped - 13 lines]
> 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:

> Regex.Replace(Research.Text, textboxSearch.Text, New
> MatchEvaluator(HiLite), RegexOptions.IgnoreCase)
[quoted text clipped - 3 lines]
> Return "<b>" + match.Value + "</b>*"
> End Function

or

> Regex.Replace(Research.Text, textboxSearch.Text, New
> MatchEvaluator(MyClassName.HiLite), RegexOptions.IgnoreCase)
[quoted text clipped - 3 lines]
> Return "<b>" + match.Value + "</b>*"
> End Function

--
Jesse Houwing
jesse.houwing at sogeti.nl

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.