7 Replies - 296 Views - Last Post: 07 December 2019 - 07:36 PM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 637
  • View blog
  • Posts: 4,226
  • Joined: 12-January 10

value pair replacing all words like instead of just the word

Posted 07 December 2019 - 08:37 AM

so i am trying to make an editor which you can find replace using a dictionary. The issue is that it is replacing every instance of the value pair no matter where it is .
example:

sentence : This is

Value pair {is, cow}

result:  Th cow cow

the result should be : This cow


here is my code:

System.Web.UI.HtmlControls.HtmlTextArea txtInput = (System.Web.UI.HtmlControls.HtmlTextArea)Page.FindControl("txtInput");
            string inputString = txtInput.InnerText;

            StringBuilder outputString = new StringBuilder();
            char[] separ = { ' ' };

            foreach (string inText in inputString.Split(separ))
            {

                foreach (string valuePair in dfindReplace.Keys)
                {
                   
                   
                   outputString.Append(inText.Replace(valuePair, " " + dfindReplace[valuePair] + " "));

                  
                }
               

            }
            




thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: value pair replacing all words like instead of just the word

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15506
  • View blog
  • Posts: 62,083
  • Joined: 12-June 08

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 10:02 AM

There's a few ways to do this.

The long route, and I can imagine a complicated linq statement would do.. but basically you needed to go a step further to do the compare on the words as each whole and not 'contains'.

            Dictionary<string, string> bar = new Dictionary<string, string>();
            bar.Add("is", "cow");          

            string input = "This is a tiny cow. Is that right?";

            string[] foo = input.Split(' ');

            string ret = string.Empty;

            foreach (string temp in foo)
            {
                foreach (KeyValuePair<string, string> i in bar)
                {
                    
                    if (temp.Equals(i.Key, StringComparison.OrdinalIgnoreCase))// can remove the ignore case if that's cool.
                        ret += i.Value + " ";
                    else
                        ret += temp + " ";

                }
            }

            Console.WriteLine(ret);

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 02:53 PM

Although, I prefer not to use regular expressions whenever possible, this is an instance when it does make the code a bit easier to understand. (Excuse any errors in the following code, I'm just doodling on the keyboard.)

var text = " " + txtInput.InnerString + " ";
foreach (KeyValuePair entry in findReplaceMap)
{
    var pattern = String.Format(@"(\W)({0})(\W)", entry.Key);
    var replacement = String.Format("$1{0}$3", entry.Value);
    text = Regex.Replace(text, pattern, replacement, RegexOptions.CaseInsensitive);
}
output = text.Sub_string(1, text.Length - 2);



(The Sub_string is to workaround the forum bug the blocks posting when it sees a method or function named substring.)

(Sorry, the name dfindReplace was making my eyes bleed. I renamed it to findReplaceMap. The .NET naming guidelines recommends not using Hungarian notation or embedding the type into variable names.)

Anyway the basic idea is to iterate over your dictionary. For each of the entries found in the dictionary, create a regular expression search pattern that wraps the text to be found with non-word characters (\W). Replace what was found with the new text. The parenthesis is mean to capture the what was found. The $1 and $3 are meant to preserve the non-word characters which were captured using the parenthesis.

The extra spaces added at the beginning and end are to keep the regular expression as simple as possible -- avoid adding special cases for the beginning and end of string where a word maybe found. The extra spaces are removed at the end with the substring call.
Was This Post Helpful? 1
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 637
  • View blog
  • Posts: 4,226
  • Joined: 12-January 10

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 03:03 PM

what happens if the word has a symbol next to it though?

let's say

the word is happy.

and in your dictionary you use {happy, sad}

the dictionary would say that happy. doesnt match happy because of the .
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 03:19 PM

Unlike most folks who use \s (whitespace) to try to delimit words, I am using \W (non-word characters).
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 03:30 PM

Oh cool! I didn't know C#'s regular expressions also know about word boundaries. That would simplify the code from post #3 to:
var text = txtInput.InnerString;
foreach (KeyValuePair entry in findReplaceMap)
    text = Regex.Replace(text, $@"\b{entry.Key}\b", entry.Value, RegexOptions.CaseInsensitive);


Was This Post Helpful? 1
  • +
  • -

#7 DarenR   User is offline

  • D.I.C Lover

Reputation: 637
  • View blog
  • Posts: 4,226
  • Joined: 12-January 10

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 03:39 PM

once i removed the text.Sub_string(1, text.Length - 2); it worked. the reason i removed it was because the substring would remove the 1st letter of the sentence.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: value pair replacing all words like instead of just the word

Posted 07 December 2019 - 07:36 PM

For post #6, there is no need for the substring since it doesn't artificially add the spaces at the beginning and end like in post #3.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1