Java 正则 多子串 匹配 替换

public static void main(String[] args) {

//被替换关键字的的数据源

   Map tokens = new HashMap();

   tokens.put("cat", "Garfield");

   tokens.put("beverage", "coffee");

 

   //匹配类似velocity规则的字符串

   String template = "#{cat} really needs some #{beverage}.";

   //生成匹配模式的正则表达式

   //String patternString = "\\$\\{(" + StringUtils.join(tokens.keySet(), "|") + ")\\}";

   String patternString = "\\#\\{(cat|beverage)\\}";

   Pattern pattern = Pattern.compile(patternString);

   Matcher matcher = pattern.matcher(template);

 

   //两个方法:appendReplacement, appendTail

   StringBuffer sb = new StringBuffer();

   while(matcher.find()) {

       matcher.appendReplacement(sb, tokens.get(matcher.group(1)));

   }

   matcher.appendTail(sb);

 

   //out: Garfield really needs some coffee.

   System.out.println(sb.toString());

 

   //对于特殊含义字符"\","$",使用Matcher.quoteReplacement消除特殊意义

   matcher.reset();

   //out: cat really needs some beverage.

   System.out.println(matcher.replaceAll("$1"));

   //out: $1 really needs some $1.

   System.out.println(matcher.replaceAll(Matcher.quoteReplacement("$1")));

 

   //到得邮箱的前缀名。插一句,其实验证邮箱的正则多种多样,根据自己的需求写对应的正则才是王道

   String emailPattern = "^([a-z0-9_\\.\\-\\+]+)@([\\da-z\\.\\-]+)\\.([a-z\\.]{2,6})$";

   pattern = Pattern.compile(emailPattern);

   matcher = pattern.matcher("[email protected]");

   //验证是否邮箱

   System.out.println(matcher.find());

   //得到@符号前的邮箱名  out: test

   System.out.println(matcher.replaceAll("$1"));

 

   //获得匹配值

   String temp = "";

   pattern = Pattern.compile("android:(name|value)=\"(.+?)\"");

   matcher = pattern.matcher(temp);

   while(matcher.find()) {

       //out: appid, joy

       System.out.println(matcher.group(2));

   }

}

你可能感兴趣的:(java,移动开发)