java正则表达式示例

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

public class RegexMatches
{
     
   private static String REGEX = "PERSON_NAME|ORG_NAME|ADDR_NAME";
   private static String INPUT = "今天PERSON_NAME要去ADDR_NAMEORG_NAME出差";
   public static void main(String[] args) {
     
      Pattern p = Pattern.compile(REGEX);
      // 获取 matcher 对象
      Matcher m = p.matcher(INPUT);
      StringBuffer sb = new StringBuffer();
      while(m.find()){
     
         m.appendReplacement(sb, " " + m.group() + " ");
      }   
      m.appendTail(sb);
      String res = sb.toString();
      res.replaceAll("\+", " ");
      System.out.println(sb.toString());
   }   
}

你可能感兴趣的:(java)