Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avi

    Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

    Hi all,

    I'm using string Replace(string oldValue, string newValue) and would like it
    to replace only full words that matches oldValue and not when oldValue is a
    substring of a larger word.

    How can it be done?

    Thanks,
    Avi


  • Martin Honnen

    #2
    Re: Replace only full words that matches oldValue and not when oldValueis a substring of a larger word

    Avi wrote:
    I'm using string Replace(string oldValue, string newValue) and would like it
    to replace only full words that matches oldValue and not when oldValue is a
    substring of a larger word.
    >
    How can it be done?
    In a regular expression pattern you can use \b to indicate a word
    boundary so that this code

    string example = "foo foobar barfoo foo";
    string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
    Console.WriteLi ne(result);

    outputs

    baz foobar barfoo baz


    --

    Martin Honnen --- MVP XML

    Comment

    • ThatsIT.net.au

      #3
      Re: Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

      Dim myText As String = "once upon a time in a small town"
      Dim myNewText As String = String.Empty
      Dim oldWord = "small"
      Dim newWord = "large"
      Dim words() As String = Split(myText, " ")

      For Each word In words
      myNewText += Replace(word, oldWord, newWord) & " "
      Next

      Response.Write( myNewText)

      "Avi" <rememberoti@ya hoo.comwrote in message
      news:%23R15$Wvz IHA.4004@TK2MSF TNGP03.phx.gbl. ..
      Hi all,
      >
      I'm using string Replace(string oldValue, string newValue) and would like
      it to replace only full words that matches oldValue and not when oldValue
      is a substring of a larger word.
      >
      How can it be done?
      >
      Thanks,
      Avi
      >

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: Replace only full words that matches oldValue and not when oldValueis a substring of a larger word

        ThatsIT.net.au wrote:
        Dim myText As String = "once upon a time in a small town"
        Dim myNewText As String = String.Empty
        Dim oldWord = "small"
        Dim newWord = "large"
        Dim words() As String = Split(myText, " ")
        >
        For Each word In words
        myNewText += Replace(word, oldWord, newWord) & " "
        Next
        >
        Response.Write( myNewText)
        That doesn't make it any better.

        Given this replacement:

        Dim oldWord = "all"
        Dim newWord = "none"

        you would get the string "once upon a time in a smnone town "

        Also notice the extra space added at the end...
        "Avi" <rememberoti@ya hoo.comwrote in message
        news:%23R15$Wvz IHA.4004@TK2MSF TNGP03.phx.gbl. ..
        >Hi all,
        >>
        >I'm using string Replace(string oldValue, string newValue) and would
        >like it to replace only full words that matches oldValue and not when
        >oldValue is a substring of a larger word.
        >>
        >How can it be done?
        >>
        >Thanks,
        >Avi
        >>
        >

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Replace only full words that matches oldValue and not when oldValueis a substring of a larger word

          Avi wrote:
          Hi all,
          >
          I'm using string Replace(string oldValue, string newValue) and would like it
          to replace only full words that matches oldValue and not when oldValue is a
          substring of a larger word.
          >
          How can it be done?
          >
          Thanks,
          Avi
          >
          As Martin suggested, the /b code in a regular expressions is the solution.

          Put the word to look for in a regular expression by using the Escape method:

          string pattern = "/b" + Regex.Escape(ol dValue) + "/b";

          Now you can use that to match the words:

          text = Regex.Replace(t ext, pattern, newValue);

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          • Duggi

            #6
            Re: Replace only full words that matches oldValue and not whenoldValue is a substring of a larger word

            On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.co mwrote:
            Avi wrote:
            Hi all,
            >
            I'm using string Replace(string oldValue, string newValue) and would like it
            to replace only full words that matches oldValue and not when oldValue is a
            substring of a larger word.
            >
            How can it be done?
            >
            Thanks,
            Avi
            >
            As Martin suggested, the /b code in a regular expressions is the solution.
            >
            Put the word to look for in a regular expression by using the Escape method:
            >
            string pattern = "/b" + Regex.Escape(ol dValue) + "/b";
            >
            Now you can use that to match the words:
            >
            text = Regex.Replace(t ext, pattern, newValue);
            >
            --
            Göran Andersson
            _____http://www.guffa.com


            Thanks for the help.

            string example = "foo foobar barfoo foo";
            string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
            Console.WriteLi ne(result);
            Works out nicely.


            -Cnu

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: Replace only full words that matches oldValue and not whenoldValue is a substring of a larger word

              Duggi wrote:
              On Jun 15, 8:11 pm, Göran Andersson <gu...@guffa.co mwrote:
              >Avi wrote:
              >>Hi all,
              >>I'm using string Replace(string oldValue, string newValue) and would like it
              >>to replace only full words that matches oldValue and not when oldValue is a
              >>substring of a larger word.
              >>How can it be done?
              >>Thanks,
              >>Avi
              >As Martin suggested, the /b code in a regular expressions is the solution.
              >>
              >Put the word to look for in a regular expression by using the Escape method:
              >>
              >string pattern = "/b" + Regex.Escape(ol dValue) + "/b";
              >>
              >Now you can use that to match the words:
              >>
              >text = Regex.Replace(t ext, pattern, newValue);
              >>
              >--
              >Göran Andersson
              >_____http://www.guffa.com
              >
              >
              >
              Thanks for the help.
              >
              string example = "foo foobar barfoo foo";
              string result = Regex.Replace(e xample, @"\bfoo\b", "baz");
              Console.WriteLi ne(result);
              Works out nicely.
              >
              >
              -Cnu
              Yes, that works if you have full control over the input, so that you can
              be sure that it doesn't contain any characters that have a special
              meaning in a regular expression.

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • Avi

                #8
                Re: Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

                Thank you all for your help!

                Avi


                "Avi" <rememberoti@ya hoo.comwrote in message
                news:%23R15$Wvz IHA.4004@TK2MSF TNGP03.phx.gbl. ..
                Hi all,
                >
                I'm using string Replace(string oldValue, string newValue) and would like
                it to replace only full words that matches oldValue and not when oldValue
                is a substring of a larger word.
                >
                How can it be done?
                >
                Thanks,
                Avi
                >

                Comment

                • ThatsIT.net.au

                  #9
                  Re: Replace only full words that matches oldValue and not when oldValue is a substring of a larger word

                  Yes your right, didn't think it though, A regex is the way to go.


                  "Göran Andersson" <guffa@guffa.co mwrote in message
                  news:%23RJwJmvz IHA.2208@TK2MSF TNGP04.phx.gbl. ..
                  ThatsIT.net.au wrote:
                  > Dim myText As String = "once upon a time in a small town"
                  > Dim myNewText As String = String.Empty
                  > Dim oldWord = "small"
                  > Dim newWord = "large"
                  > Dim words() As String = Split(myText, " ")
                  >>
                  > For Each word In words
                  > myNewText += Replace(word, oldWord, newWord) & " "
                  > Next
                  >>
                  > Response.Write( myNewText)
                  >
                  That doesn't make it any better.
                  >
                  Given this replacement:
                  >
                  Dim oldWord = "all"
                  Dim newWord = "none"
                  >
                  you would get the string "once upon a time in a smnone town "
                  >
                  Also notice the extra space added at the end...
                  >
                  >"Avi" <rememberoti@ya hoo.comwrote in message
                  >news:%23R15$Wv zIHA.4004@TK2MS FTNGP03.phx.gbl ...
                  >>Hi all,
                  >>>
                  >>I'm using string Replace(string oldValue, string newValue) and would
                  >>like it to replace only full words that matches oldValue and not when
                  >>oldValue is a substring of a larger word.
                  >>>
                  >>How can it be done?
                  >>>
                  >>Thanks,
                  >>Avi
                  >>>
                  >>
                  >
                  >
                  --
                  Göran Andersson
                  _____
                  http://www.guffa.com

                  Comment

                  Working...