10 Replies Latest reply: Jul 15, 2009 3:52 PM by 807588 RSS

    RegEx replaceAll question

    807588
      I want to replace all instances of a word only if it's not part of another word. So if I have:

      //I want to replace "Word" with String
      private String myWord;
      private Word someWord;
      
      Word newWord = (Word) other;
      It should become:
      private String myWord;
      private String someWord;
      
      String newWord = (String) other;
      So far I have the regex expression: "\\WWord\\W" which finds them all (properly ignoring "MyWord" for example) but it also replaces the non-word character after and before it. So it incorrectly (incorrectly for my needs) changes "(Word)" into "String" without the prentheses. How would I use replaceAll (in Matcher) to do this?
        • 1. Re: RegEx replaceAll question
          807588
          Hi 6tr6tr,

          You may use the refactoring utility of your IDE if you mean to modify the type of a variable in your class. It takes only a few clicks to do the job.
          • 2. Re: RegEx replaceAll question
            796365
            Define what "it's not part of another word" means, exactly.
            • 3. Re: RegEx replaceAll question
              807588
              ChuckBing wrote:
              Define what "it's not part of another word" means, exactly.
              Not preceeded or appended by [a-zA-Z_0-9].

              The best I was able to come up with is this:
              String str = "notWordNameButIsWord = s\npublic void Word x;\nWord x = (Word)s;";
                        
              Pattern p = Pattern.compile( "\\W(Word)\\W" );
              Matcher m = p.matcher( str );
                        
              while ( m.find() )
              {
                   String next = m.group();
                             
                   str = str.replace( next, next.replace( "Word", "String" ) );
              }
              System.out.println( str );
              • 4. Re: RegEx replaceAll question
                796365
                Try this
                String str = "notWordNameButIsWord = s\\npublic void Word x;\\nWord x = (Word)s;";
                str = str.replaceAll("(\\W)Word(\\W)", "$1String$2");
                System.out.println(str);
                Note that I doubled the backslashes that were in the string you posted, assuming that you wanted the string to include single backslashes at each position. When you create a string as above, a single backslash will be read as a newline - so it's necessary to double them so they don't act as a newline.

                Edited by: ChuckBing on Jul 14, 2009 4:44 PM

                NB: The $n construct in the replacement is shown in the Matcher.appendReplacement metod documentation.
                • 5. Re: RegEx replaceAll question
                  807588
                  str = str.replaceAll("\\bWord\\b", "String");
                  \b is the word-boundary construct: a zero-width assertion that matches a position that's either followed by a word character and not preceded by one (start of word), or preceded by a word character and not followed by one (end of word). Since it doesn't consume any characters, it allows you to match all three "Word"s in
                  "Word Word Word"
                  where
                  "(\\W)Word(\\W)"
                  will only match the middle one.
                  • 6. Re: RegEx replaceAll question
                    796365
                    Yech, limit cases!

                    Thanks for the explanation uncle_alice.

                    How are word boundary [\b] and non-word boundary [\B] defined? I can't find a definition for Java's regexes.
                    • 7. Re: RegEx replaceAll question
                      807588
                      [Here you go.|http://www.regular-expressions.info/wordboundaries.html]
                      • 8. Re: RegEx replaceAll question
                        807588
                        uncle_alice wrote:
                        Here you go.
                        You're not Jan Goyvaerts by the way, are you?
                        • 9. Re: RegEx replaceAll question
                          807588
                          ChuckBing wrote:
                          Try this
                          String str = "notWordNameButIsWord = s\\npublic void Word x;\\nWord x = (Word)s;";
                          str = str.replaceAll("(\\W)Word(\\W)", "$1String$2");
                          System.out.println(str);
                          Note that I doubled the backslashes that were in the string you posted, assuming that you wanted the string to include single backslashes at each position. When you create a string as above, a single backslash will be read as a newline - so it's necessary to double them so they don't act as a newline.

                          Edited by: ChuckBing on Jul 14, 2009 4:44 PM

                          NB: The $n construct in the replacement is shown in the Matcher.appendReplacement metod documentation.
                          THANK YOU to both you (ChuckBing) and uncle_alice!! Putting your suggestions together gave me the perfect solution! :)
                          • 10. Re: RegEx replaceAll question
                            807588
                            Encephalopathic wrote:
                            You're not Jan Goyvaerts by the way, are you?
                            Thanks, but I don't even know how to pronounce his name. :D