I had a requirement to change AssemblyVersion on new build. I do it using java code string.replaceAll(regexPattern,updatedString);
This code works fine with normal regex patterns, but I am not able to use non-capturing groups in this pattern. I want to use non-capturing groups to make sure I don't capture patterns other than required one. This is the code I tried:
String str="[assembly: AssemblyVersion(\"1.0.0.0\")]";
str=str.replaceAll("(?:\\[assembly: AssemblyVersion\\(\"\\d\\.\\d\\.)?.*(?:\"\\)\\])?", "4.0");
System.out.println(str);
Here, I want to match string [assembly: AssemblyVersion(int.int)] and replace only minor version.
Expected outcome is [assembly: AssemblyVersion("1.0.4.0")], but I'm getting result as 4.04.0.
Can anyone please help me on this?
解决方案
Why not use look-ahead / look-behind instead?
They are non-capturing and would work easily here:
str = str
.replaceAll(
"(?<=\\[assembly: AssemblyVersion\\(\"\\d\\.\\d\\.).*(?=\"\\)\\])",
"4.0"
);