Issue
I'm trying to figure if it is possible to write a regex expression that matches a path ignoring some parts of this path, lets see some example
full path >> simple path
SomeNameA.{key.val}/SomeNameAB.{key.val} >> SomeNameA/SomeNameAB
SomeNameY.{key.val}{key2.val2}/XYZ/SomeOtherPath.{key.val} >> SomeNameY/XYZ/SomeOtherPath
basically I want to discard everything after the first "." (dot) until the "/". Right now I manually doing the split in the "/" then, finding the position of "." and doing a substring of it, manually matching it.
So later when I receive the object with the full path, I can check if we have something configure for it with the simple path
for simplicity, lets say I'm trying something similar to this:
String fullPath = "SomeNameY.{key.val}{key2.val2}/XYZ/SomeOtherPath.{key.val}";
String simplePath = "SomeNameY/XYZ/SomeOtherPath";
if (!fullPath.match(simplePath)) return;
In this example I'm expecting it to return true so I can continue with the setup of that particular item.
For this case scenario I'm looking for an exact match, case sensitive and nothing else between or after it.
So this "A.{key.val}/B" should only match "A/B" and not match this "A/b" or this "A/z/B".
Sure this full path "A.{key.val}/B" and "A.{key.val}/B.{key.val}" will have the same simple version but this won't happen by design, so no concern about it.
Solution
Following @Pshemo comment, using the regex to replace the unwanted parts of the path works, so instead of try to make them match with the regex, it just remove the extra characters and do a normal comparison over the result
String simplePath = "SomeNameY/XYZ/SomeOtherPath";
String fullPath = "SomeNameY.{key.val}{key2.val2}/XYZ/SomeOtherPath.{key.val}";
String simplifiedPath = fullPath.replaceAll("\\.?\\{.*?\\}","");
if (!simplePath.equals(simplifiedPath)) return;
Answered By - P. Waksman
Answer Checked By - Terry (JavaFixing Volunteer)