I have strings of the format
/person/PATH_VARIABLE/address/PATH_VARIABLE
/person/PATH_VARIABLE/PATH_VARIABLE
/person/PATH_VARIABLE/address
.....
etc
I need to replace the PATH_VARIABLE with regular expression so that it allows me to match anything between separator / or nothing at end so that when I match the regex string with my input String I have a complete match
/person/abc/address/xy123 matches with the first
/person/abc/1233 matches with the second
I have tried a few things
public static void main(String[] args) {
// Sample Strings to be subtituted
String y = "/person/PATH_VARIABLE/address/PATH_VARIABLE";
String y1 = "/person/PATH_VARIABLE/PATH_VARIABLE";
// Tried this
//y = y.replaceAll("PATH_VARIABLE", "\\((.*?)\\)");
//y1 = y1.replaceAll("PATH_VARIABLE", "\\((.*?)\\)");
// Tried this one
y = y.replaceAll("PATH_VARIABLE", "(?<=/)(.*?)(?=/?)");
y1 = y1.replaceAll("PATH_VARIABLE", "(?<=/)(.*?)(?=/?)");
// Sample input strings to match
String x = "/person/user.zian/address/123";
String x1 = "/person/nhbb/bhbhb/ghyu";
String x2 = "/person/nhbb/bhbhb";
System.out.println(x.matches(y)); // returns true
System.out.println(x1.matches(y)); // returns false
System.out.println(x1.matches(y1)); // returns true but should return false
System.out.println(x2.matches(y1)); // returns true
}