C# my custom ReplaceWholeWord extension method not working properly

T.Zacks 3,996 Reputation points
2023-01-25T19:56:22.4333333+00:00

Objective: I have some string stored within double quote which i need to replace with some other input if text match.

see my working code first where i used Replace function.

string replaced = "";
string actualformula = "\"Operational Metrics~Mainline ASM\"/\"Operational Metrics~Mainline Fuel Consumption (In Gallons)\"";
replaced = actualformula.Replace("Operational Metrics~Mainline ASM", "AA10");
replaced= actualformula.Replace("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");

the above code perfectly replace.

see now the same code where i am trying to replace with custom extension method which not able to replace this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10

see extension method code

public static class Extension
{
	public static string ReplaceWholeWord(this string original, string wordToFind, string  replacement, RegexOptions regexOptions = RegexOptions.None)
	{
		string pattern = String.Format(@"\b{0}\b", wordToFind);
		string ret = Regex.Replace(original, pattern, replacement, regexOptions);
		return ret;
	}
}

see actual code where i am using ReplaceWholeWord extension method which not able to replace this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10

private void button2_Click(object sender, EventArgs e)
{
	string replaced = "";
	string actualformula = "\"Operational Metrics~Mainline ASM\"/\"Operational Metrics~Mainline Fuel Consumption (In Gallons)\"";
	replaced = actualformula.ReplaceWholeWord("Operational Metrics~Mainline ASM", "AA10");
	replaced= actualformula.ReplaceWholeWord("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");
}

please guide me what is wrong in my approach for which code not able to replace this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10

Tried this way also did not worked.

private void button2_Click(object sender, EventArgs e)
{
	string replaced = "";
	//string actualformula = ""Operational Metrics~Mainline ASM"/"Operational Metrics~Mainline Fuel Consumption (In Gallons)"";
	string actualformula = """+ "Operational Metrics~Mainline ASM" + """ + "/"+ """+"Operational Metrics~Mainline Fuel Consumption (In Gallons)"+ """;
	replaced = actualformula.ReplaceWholeWord("Operational Metrics~Mainline ASM", "AA10");
	replaced= actualformula.ReplaceWholeWord("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");
}

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.6K Reputation points
    2023-01-25T20:12:46.25+00:00

    Try an adjusted function:

    public static class Extension
    {
        public static string ReplaceWholeWord( this string original, string wordToFind, string replacement, RegexOptions regexOptions = RegexOptions.None )
        {
            string pattern = string.Format( "\"{0}\"", Regex.Escape( wordToFind ) );
            string ret = Regex.Replace( original, pattern, '"' + replacement + '"', regexOptions );
    
            return ret;
        }
    }
    

    Maybe you can also use the initial idea, a simple string.Replace, instead of Regex.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 70,776 Reputation points
    2023-01-25T21:11:39.1266667+00:00

    your issue is the special chars ( and ) in the pattern built from the word to find. they should be escaped.

    	replaced= actualformula.ReplaceWholeWord("Operational Metrics~Mainline Fuel Consumption \\(In Gallons\\)", "BB10");
    

    but you have an additional problem, as ")" is not a word boundary character and is the last character in the match, the \b makes the match fail, so you need to use a different match then \b for the end.

    see this tool to build expression

    [https://regex101.com/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.