Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Method case insensitive

Status
Not open for further replies.

wau25

Programmer
Mar 12, 2004
21
US
This is what I do with ASP3.0.

If a user enters "This is green color"
I want to first search if the word "green" color exists then replace the word "green" with <font color=green>green</font>. When searching for the word I don't need it to be case sensitive and when replacing the word I want to preserve the case. For exmple, if user enters "This is GrEEn color" I want to replace the word GrEEn with <font color=GrEEn>GrEEn</font>.

This is pretty easy with VB's case insensitive instr and replace but I can't seem to find a simple solution in C#
 
wau: I program in VB but here's a tidbit from the Visual Studio IDE help file:

Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.

[C#]
[Serializable]
public string Replace(
string input,
MatchEvaluator evaluator,
int count,
int startat
);

Parameters
input
The string to be modified.

evaluator
The MatchEvaluator which evaluates replacement at each step.

count
The maximum number of times the replacement will occur.

startat
The character position in the input string where the search will begin.

Return Value
The modified character string.

Remarks

The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);The delegate is called once per match during a replace.

 
Give these a try,
This will match "green" ignoring case
Code:
Match MyRegMatch = Regex.Match(_input,"green",RegexOptions.IgnoreCase); 
if(MyRegMatch.Success)  {
    Console.WriteLine("good string" + _input);
}

This will split on the match "green" ignoring case and return an array of string not contain the match string
Code:
string[] s_array = Regex.Split(_input,"green",RegexOptions.IgnoreCase);
Marty
 
Just thought of something, you do not need a split, after you get the match just use the Replace method of the string class.
Marty
 
I like PERL so I enjoyed this post.
If the match is successful the Value has the match and you can use $& in the replacement string which contains the match.
If the s_input is, I like grEEn this
Code:
Match MyRegex = Regex.Match(s_input,"green",RegexOptions.IgnoreCase); 
if (MyRegex.Success) {
	string s_output = Regex.Replace(_input,MyRegex.Value,"<font color=\"green\">$&</font>"); 
	Console.WriteLine(s_output);
}
will produce, I like <font color="green">grEEn</font>

You can really do some good stuff with capturing parentheses and the Gruops collection.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top