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;


/**
 *
 * @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 = "
";
  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("
"),
    link,
    Util.SUBSTITUTE_ALL);
  System.out.println(result);
  
 }

}

  • jakarta-oro-2.0.1.zip (702.5 KB)
  • 下载次数: 2

你可能感兴趣的:(java,正则表达式,替换,perl)