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:
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, ""); }
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
Post a Comment