Deal with apostrophe in java regex in replaceALL -


trying replace exact & whole occurrences of pattern using following code. apparently in you'll being replaced @@@'ll. want you replaced.

please suggest.

import java.util.*; import java.io.*; public class fielreadingtest{   public static void main(string[] args) throws ioexception {     string mytext  = "i knew long before met you. know you’re awesome person. way you’ll missed. ";      string newline = system.getproperty("line.separator");     system.out.println("before:" + newline + mytext);     string pattern = "\\byou\\b";     mytext = mytext.replaceall(pattern, "@@@");     system.out.println("after:" + newline +mytext);  } }  /* before: knew long before met you. know you’re awesome person. way you’ll missed.  after: knew @@@ long before met @@@. know @@@’re awesome person. way @@@’ll missed.   */ 

this being said have input file contains list of words want skip looks this: enter image description here

now per @anubhav have use (^|\\s)you([\\s.]|$) replace you not else. best bet use tool notepad++ , pre & post fix input words above or change in code itslef. code i'm using this:

  (string pattern : patternstoskip) {      line = line.replaceall(pattern, "");    } 

source: https://www.cloudera.com/content/cloudera-content/cloudera-docs/hadooptutorial/cdh4/hadoop-tutorial/ht_wordcount2_source.html?scroll=topic_7_1

you can instead use regex:

string pattern = "(^|\\s)you([\\s.,;:-]|$)"; 

this match "you" at:

  • start or preceded space
  • end or followed space or listed punctuation characters

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -