java正则表达式的分组,java正则表达式分组替换实现,java正则表达式分组,java正则表达式分组替...

本文介绍了如何在Java中实现正则表达式的分组替换,由于Java本身不直接支持此类功能,作者ElliottHughes提供了一个名为Rewriter的抽象类来实现这一机制。Rewriter类通过重写replacement()方法来计算每个匹配项的替换值,使用group()方法获取分组捕获的内容。示例代码展示了如何创建一个将数字乘以3的替换规则,并应用于字符串。
摘要由CSDN通过智能技术生成

java正则表达式分组替换实现,java正则表达式分组,java正则表达式分组替

java正则表达式分组替换实现

java并没有提供类似csharp的正则表达式分组替换机制,要做高级的正则表达式分组替换需要自己实现。

所幸的是Elliott Hughes已经实现了java的正则表达是分组替换。

下面是它的实现代码:import java.util.regex.*;/** * A Rewriter does a global substitution in the strings passed to its * 'rewrite' method. It uses the pattern supplied to its constructor, and is * like 'String.replaceAll' except for the fact that its replacement strings * are generated by invoking a method you write, rather than from another * string. This class is supposed to be equivalent to Ruby's 'gsub' when * given a block. This is the nicest syntax I've managed to come up with in * Java so far. It's not too bad, and might actually be preferable if you * want to do the same rewriting to a number of strings in the same method * or class. See the example 'main' for a sample of how to use this class. * * @author Elliott Hughes */public abstract class Rewriter{ private Pattern pattern; private Matcher matcher; /** * Constructs a rewriter using the given regular expression; the syntax is * the same as for 'Pattern.compile'. */ public Rewriter(String regex) { this.pattern = Pattern.compile(regex); } /** * Returns the input subsequence captured by the given group during the * previous match operation. */ public String group(int i) { return matcher.group(i); } /** * Overridden to compute a replacement for each match. Use the method * 'group' to access the captured groups. */ public abstract String replacement(); /** * Returns the result of rewriting 'original' by invoking the method * 'replacement' for each match of the regular expression supplied to the * constructor. */ public String rewrite(CharSequence original) { this.matcher = pattern.matcher(original); StringBuffer result = new StringBuffer(original.length()); while (matcher.find()) { matcher.appendReplacement(result, ""); result.append(replacement()); } matcher.appendTail(result); return result.toString(); } public static void main(String... args) throws Exception { String str = "12 54 1 65"; // anonymous subclass Rewriter tripler = new Rewriter("(\\d{1,2})") { public String replacement() { int intValue = Integer.valueOf(group(1)); return String.valueOf(intValue * 3); } }; System.out.println(tripler.rewrite(str)); // inline subclass System.out.println(new Rewriter("(\\d{1,2})") { public String replacement() { int intValue = Integer.valueOf(group(1)); return String.valueOf(intValue * 3); } }.rewrite(str)); }}

Rewriter类是一个抽象类,我们需要根据需要实现replacement()方法,该方法会根据正则匹配的分组的结果返回要替换的值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值