RegEx upper case swap

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I use RegEx to search for a number of strings and replace them with
upper case versions of themselves?

Thanks
 
Hi,
You can try something like this:
private void Form1_Load(object sender, EventArgs e)
{
string[] sentences =
{
"cow over the moon",
"Betsy the Cow",
"cowering in the corner",
"no match here"
};

string sPattern = "cow";

foreach (string s in sentences)
{
System.Console.Write("{0,24}", s);

if (System.Text.RegularExpressions.Regex.IsMatch(s,
sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
MessageBox.Show ( sPattern.ToUpper());
}
else
{
MessageBox.Show("");
}
}

}
ref:http://msdn2.microsoft.com/en-us/library/ms228595(VS.80).aspx
 
Hello Bob,
How can I use RegEx to search for a number of strings and replace them
with upper case versions of themselves?

Thanks

You cannot use a replacement pattern to indicate you want the found match
to become upper case. You can however use a MatchEvaluator to accomplish
this:

static Regex rx = new Regex("\b(stringa|stringb|stringc)\b", RegexOptions.Compiled
| RegexOptions.IgnoreCase);

public string UpperSpecialWords(string input)
{
return rx.Replace(input, new MatchEvaluator(UpperSpecialWordsImpl));
}

private string UpperSpecialWordsImpl(Match m)
{
return m.Value.ToUpper();
}

In the MatchEvaluator function you can manipulate the contents of the match
and return them as a string. These changes will be merged with the original
resultign in exactly what you need.

Jesse
 

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

Similar Threads

RegEx Help 2
Regex in C# 4
Regex woes 8
Rookie thoughts on Regex--useful but not complete 28
regex multiplication problem 3
Regex password 2
regex replace question 1
Newbie question about Regex 8

Back
Top