java正则表达式快速替换指定文本

package org.jkt.demo;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Perl5Substitution;
import org.apache.oro.text.regex.Util;


/**
 * <a href="http://xxxx.com/interface.html#$1?param=2">
 * @author micon
 *
 */
public class ReplaceDemo {
 
 public static void main(String[] args) throws MalformedPatternException {
  
//   String rawStr = "2008-08-11 17:16:32"; 
//      Perl5Matcher matcher = new Perl5Matcher(); 
//      Pattern pattern = new Perl5Compiler().compile("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2}:\\d{2})"); 
//      String result = Util.substitute(matcher, pattern, new Perl5Substitution("变换 $3,$2,$1 $4"), rawStr, Util.SUBSTITUTE_ALL); 
//      System.out.println("格式yyyy-MM-dd HH:mm:ss到dd,MM,yyyy HH:mm:ss"); 
//      System.out.println(result); 
  
  String link = "<a href=\"http://xxx.com/interface.html#how\">";
  String regexpForLink = "<\\s*a\\s+href\\s*=\\s*\"http://xxx.com/interface.html#([^\"]+)\">";
  
  PatternCompiler compiler = new Perl5Compiler();
  Pattern pattertForLink = compiler.compile(regexpForLink,Perl5Compiler.CASE_INSENSITIVE_MASK);
  
  PatternMatcher matcher = new Perl5Matcher();
  
  String result = Util.substitute(matcher, pattertForLink,
    new Perl5Substitution("<a href=\"http://newserver.com/interface.html#$1\">"),
    link,
    Util.SUBSTITUTE_ALL);
  System.out.println(result);
  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值