How to use regex non capturing group for string replace in java -
i had requirement change assemblyversion
on new build. using java code string.replaceall(regexpattern,updatedstring);
this code works fine normal regex patterns, not able use non-capturing groups in pattern. want use non-capturing groups make sure don't capture patterns other required one. code tried:
string str="[assembly: assemblyversion(\"1.0.0.0\")]"; str=str.replaceall("(?:\\[assembly: assemblyversion\\(\"\\d\\.\\d\\.)?.*(?:\"\\)\\])?", "4.0"); system.out.println(str);
here, want match string [assembly: assemblyversion(int.int
, replace minor version.
expected outcome [assembly: assemblyversion("1.0.4.0")]
, i'm getting result 4.04.0
.
can please me on this?
why not use looka-head / look-behind instead?
they non-capturing , work here:
str = str .replaceall( "(?<=\\[assembly: assemblyversion\\(\"\\d\\.\\d\\.).*(?=\"\\)\\])", "4.0" );
Comments
Post a Comment