athlon
发贴: 18
积分: 1
|
于 2005-09-29 17:15
我在网上看个一篇文章是关于正则表达式的替换方法,用的是Util.substitute()方法,地址:http://www.51cto.com/html/2005/0915/3118.htm ,结合这篇文章我写了一个方法,现贴出来,让大家看看: /** * @param source 源字符串 * @param reg 正则表达式 "/(/*\\-*\\w)*?\\.gif" * @param dir 需要加的字符串 * @return */ static String replace(String source,String reg,String dir) { String changestring = ""; String numberExpression = "("+reg+")"; Pattern pattern = null; PatternCompiler compiler; PatternMatcher matcher; MatchResult result; // Create Perl5Compiler and Perl5Matcher instances. compiler = new Perl5Compiler(); matcher = new Perl5Matcher();
try { pattern = compiler.compile(numberExpression); } catch(MalformedPatternException e) { } Perl5Substitution p5s = new Perl5Substitution( dir+"$1",0); changestring = Util.substitute(matcher,pattern,p5s,source,Util.SUBSTITUTE_ALL); System.out.println("test:"+changestring); return changestring; } 用的包是jakarta-oro-2.0.8.jar,有不足之处望大家指出;非常感谢why,给我的提示;
|