none
Replace function failed to replace whole word RRS feed

  • Question

  • i saw Replace function failed to replace whole word. here is example which i solved this way

                string input = "AQ443/AQ4/4";
                string pattern = @"\AQ4\b";
                string replace = "Hello";
                string result = Regex.Replace(input, pattern, replace);
                Console.WriteLine(result);
    
                string outpt = ReplaceWholeWord(input, "AQ4", "Hello");
            public string ReplaceWholeWord(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;
            }


    Wednesday, March 20, 2019 12:52 PM

Answers

  • Given the input for your ReplaceWholeWord method you get back `AQ443/Hello/4` which seems correct to me. What are you getting back and what did you expect? Can you give other example inputs and outputs?

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Friday, March 22, 2019 4:35 PM
    Wednesday, March 20, 2019 1:45 PM
    Moderator