在replaceALL中处理java regex中的撇号

时间:2014-03-10 15:03:36

标签: java regex

尝试更换只有EXACT&使用以下代码进行模式的全部发生。显然你和你在一起被替换为@@@' ll。但我想要的只是you被替换。

请建议。

import java.util.*;
import java.io.*;
public class Fielreadingtest{


public static void main(String[] args) throws IOException {
    String MyText  = "I knew about you long before I met you. I also know that you’re an awesome person. By the way you’ll be 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:
I knew about you long before I met you. I also know that you’re an awesome person. By the way you’ll be missed. 
After:
I knew about @@@ long before I met @@@. I also know that @@@’re an awesome person. By the way @@@’ll be missed. 

*/

这就是说我有一个输入文件,其中包含我要跳过的单词列表,如下所示: enter image description here

现在根据@Anubhav,我必须使用(^|\\s)you([\\s.]|$)来完全替换you,而不是其他任何东西。我最好使用像记事本++和pre& amp; post修复我上面的所有输入单词或更改代码中的内容。我使用的代码是:

  for (String pattern : patternsToSkip) {
     line = line.replaceAll(pattern, "");
   }

来源:https://www.cloudera.com/content/cloudera-content/cloudera-docs/HadoopTutorial/CDH4/Hadoop-Tutorial/ht_wordcount2_source.html?scroll=topic_7_1

2 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式:

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

这仅在"you"匹配:

  • 以空格开头或前面
  • 结束或后跟空格或一些列出的标点字符

答案 1 :(得分:1)

您可以使用否定前瞻:

\b(you)(?!['’])

转换为Java字符串:

"\\b(you)(?!['’])"

您的演示输入包含与键盘不同的撇号。我把两者放在了负面的前瞻中。

 import  java.util.regex.Pattern;
 import  java.util.regex.Matcher;

 /**
    <P>{@code java ReplaceYouWholeWordWithAtAtAt}</P>
  **/
 public class ReplaceYouWholeWordWithAtAtAt  {
    public static final void main(String[] ignored)  {

       String sRegex = "\\byou(?!['’])";

       String sToSearch = "I knew about you long before I met you. I also know that you’re an awesome person. By the way you’ll be missed.";
       String sRplcWith = "@@@";

       Matcher m = Pattern.compile(sRegex).matcher(sToSearch);
       StringBuffer sb = new StringBuffer();
       while(m.find())  {
          m.appendReplacement(sb, sRplcWith);
       }
       m.appendTail(sb);

       System.out.println(sb);
    }
 }

输出:

[C:\java_code\]java ReplaceYouWholeWordWithAtAtAt
 I knew about @@@ long before I met @@@. I also know that youÆre an awesome person. By the way youÆll be missed.