使用 ant 替换字符串中子字符串的正则表达式

Regex for Replacing a subString in a String using ant

我一直在努力尝试在 ant 文件中使用正则表达式(使用 replaceregexp 标记)来替换特定的字符串,例如 java class 中的不是常量:
替换:V1_0_0 为 V2_0_0 在:

public void doSomething() {
    return "xxxxxxxV1_0_0.yyyyyyyy"
}

当然 V1_0_0 会一直变化 .yyyyyyyy 会改变,但 xxxxxxx 会保持不变

这是我能得到的更接近的: (?<=xxxxxxx).* 或 (?<=xxxxxxx).*

但这就是我得到的:

public void doSomething() {
    return "xxxxxxxV2_0_0;
}

xxxxxxx 或 yyyyyyyy 可以是 java class 名称

中允许的任何字符

这样试试:

(?:xxxxxxx)V[0-9]+_[0-9]+_[0-9]+(?:\.[a-z]+)?

我使用 ?yyyyyy 部分设为可选。 也许你需要一个不同于 a-z 的字符 class,也许 [a-zA-Z][a-zA-Z0-9_].

Demo

Code Sample:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone {
 public static void main(String[] args) throws java.lang.Exception {
  String regex = "(?:xxxxxxx)V[0-9]+_[0-9]+_[0-9]+(?:\.[a-z]+)?";
  String string = "public void doSomething() {\n" 
                + "    return \"xxxxxxxV1_0_0.yyyyyyyy\";\n" 
                + "}";
  String subst = "xxxxxxxV2_0_0";

  Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  Matcher matcher = pattern.matcher(string);

  String result = matcher.replaceAll(subst);
  System.out.println("Substitution result: " + result);
 }
}