Java正则表达式替换并捕获组。

3

可能是重复问题:
Java Regex Replace with Capturing Group

有没有办法用捕获组的修改内容替换正则表达式?

例如:

Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher(text);
resultString = regexMatcher.replaceAll("$1"); // *3 ??

我希望用$1乘以3替换所有出现的内容。

编辑:

看起来有些问题:(

如果我使用

Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher("12 54 1 65");
try {
    String resultString = regexMatcher.replaceAll(regexMatcher.group(1));
} catch (Exception e) {
    e.printStackTrace();
}

它会抛出一个IllegalStateException: No match found异常。

但是,

Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher("12 54 1 65");
try {
    String resultString = regexMatcher.replaceAll("$1");
} catch (Exception e) {
    e.printStackTrace();
}

这个功能很好用,但是我无法更改$1 :(

编辑2:

现在,它可以正常工作了 :)


如果其他用户遇到类似的问题,你能详细说明一下你是如何纠正这个问题的吗? :) - undefined
我不知道怎么用Java来做,但是在Perl中非常容易。:-P $foo = '12 54 1 65'; $foo =~ s/(\d{1,2})/$1 * 3/eg; - undefined
这里曾经有一个回答描述了如何解决这个问题,但我现在不知道它在哪里了...(我接受了一个解决问题的答案,但它消失了。很奇怪...) - undefined
我认为这个问题的答案可以帮助你。 - undefined
2个回答

18

这个问题的最终解决方案是由Elliott Hughes在他的博客上发布了几年前。 Elliott在在线版本中不断引入其他类的无用依赖关系,因此我将在此处发布一个独立版本(依赖关系仅存在于main()方法的测试中)。

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));

  }
}

0
不,你不能用正则表达式来做这个。正则表达式没有数字值的概念,所以无法进行数字运算(假设你想将"12 54 1 65"转换为"36 162 3 195")。
请注意,某些编程语言和正则表达式实现可以做到这一点(如Chris提到的Perl),但这不是正则表达式的特性,尤其不是Java正则表达式的特性。 你说你已经解决了这个问题,所以我猜你采取了"手动"的方式,将每个匹配项转换为整数,并将其乘以3。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接