|
Hi. I think a regular expression will meet my need, but I've gone beyond my knowledge of regular expressions to do this one:
I have some text such as {2539}hello.{EOD}How are you? word, words WORDS, wordsing and more words{6354}Great day, huh!
I'm working in c# and need to be able to do a replace all on complete words.
So, if I were to swap out "words" to "frogs", the above would become: {2539}hello.{EOD}How are you? word, frogs frogs, wordsing and more frogs{6354}Great day, huh!
*note* in the replacement "wordsing" was not touched because words was found in it, but it was part of a longer word.
Two other issues: 1) it needs to be case insensitive 2) Is it possible to have the replacement text match the case of the original text?
Does anyone know how to do this?
Thank you for your help.
- Moved bynobugzMVP, ModeratorWednesday, September 09, 2009 12:21 PM (From:.NET Base Class Library)
-
| | hayedid2 | By the way, the replacement text can be different lengths... so in matching the case, I'm only concerned about the first letter (since I don't want to put a lowercase replacement word at the beginning of a sentence).
Thanks agian. | | hayedid2 | Something like this should do what you're after, though it's probably not the best:
string from = "words";
string to = "frogs";
string input = "...";
string output = Regex.Replace(input, "\\b" + from + "\\b",
delegate(Match target) {
if (string.Compare(target.Value, from.ToUpper(),
StringComparison.InvariantCulture) == 0)
{
return to.ToUpper();
}
else if (string.Compare(
target.Value,
from.Substring(0,1).ToUpper() + from.Substring(1),
StringComparison.InvariantCulture) == 0)
{
return to.Substring(0,1).ToUpper() + to.Substring(1);
}
else
{
return to;
}
}
, RegexOptions.IgnoreCase);
| | An Anon Coward | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
private static String NewValue;
static void Main(string[] args)
{
String word = "{2539}hello.{EOD}How are you? word, words WORDS, wordsing and more words{6354}Great day, huh!";
String newWord = Replace(word, "words", "songs");
Console.WriteLine(newWord);
Console.ReadLine();
}
static String Replace(String wholeString, String oldValue, String newValue)
{
NewValue = newValue;
Regex rx = new Regex(String.Format(@"\b{0}\b", oldValue), RegexOptions.IgnoreCase);
MatchEvaluator mv = new MatchEvaluator(MatchReturn);
String temp = rx.Replace(wholeString, mv);
return temp;
}
static String MatchReturn(Match m)
{
if (Char.IsUpper(m.Value.ToCharArray()[0]))
{
Char[] temp = NewValue.ToCharArray();
Boolean isUpper = true;
Char[] chars = new Char[temp.Length];
for (Int32 i = 0; i < temp.Length; i++)
{
if (i == 0)
temp[0] = Char.ToUpper(temp[0]);
else
temp[i] = temp[i];
}
foreach (Char c in m.Value.ToCharArray())
{
if (!Char.IsUpper(c))
isUpper = false;
}
if (isUpper)
{
for (Int32 i = 0; i < temp.Length; i++)
temp[i] = Char.ToUpper(temp[i]);
}
String t = new String(temp);
return t;
}
return NewValue;
}
}
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | My solution will also check to see if not only the word is upper, but it will check to see if the first word is upper. So if you wanted to replace "words" with "songs":
{2539}hello.{EOD}How are you? word, words WORDS, wordsing. Words are more words{6354}Great day, huh!";
You would get:
"{2539}hello.{EOD}How are you? word, songs SONGS, wordsing. Songs are more songs{6354}Great day, huh!";
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Generally, you canjust use regex.replace() function twice, (use case-sensitive mode) first pattern= "\bwords\b" then replace to "frogs" and then pattern = "\bWords\b" replace to "Frogs"
\b will match the boundary of a word, avoid part match. if you want to replace "WORDS" then use a third one
if you havevarious formations mixed upper and lower case characters, (though I don't think it necessary, maybe you will replace dIRE sTRAITS?) you will have to use John's checking funtion, check each one. www.wonderstudio.cn | | Eping Wang |
|