java - 使用 ant 替换字符串中的子字符串的正则表达式
问题描述
尝试在 ant 文件中使用正则表达式(使用 replaceregexp 标记)来替换 Java 类中不是常量的特定字符串时,我一直在苦苦挣扎,例如:
Replace: V1_0_0 by V2_0_0 In:
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 类名中允许的任何字符
解决方案
试试这样:
(?:xxxxxxx)V[0-9]+_[0-9]+_[0-9]+(?:\.[a-z]+)?
我yyyyyy
使用?
. a-z
也许您需要一个与, 也许[a-zA-Z]
或不同的字符类[a-zA-Z0-9_]
。
代码示例:
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);
}
}
推荐阅读
- c# - 关闭 .net core 3.1 中的 HttpClient 登录
- r - 尝试运行此代码时未定义的列选择问题
- r - 序列化错误(数据,node$con):写入与磁盘框架的连接时出错
- html - 如何在这些图标之间获得空间?
- swift - 如何在函数的每一行之后检查条件?
- love2d - 在love2d中,我遇到了碰撞检测问题
- reactjs - Ionic React:标签之间的切换非常慢,因为我添加了 Ionic
- validation - 禁用提交按钮,直到使用 JavaScript 填写必填字段
- php - PHP curl_multi_exec 没有同时启动多线程
- string - ABAP:在字符串模板中使用 if 条件