Revision d3035e7c-764a-43e4-975c-070a4f7dedbc - Code Review Stack Exchange
> Your code looks great!
Here, maybe another option that we might exercise would be to possibly do the entire task with an expression, maybe something similar to these:
^(.+?\/).+\/(.+)$
(.+?\/).+\/(.+)
Our first capturing group is non-greedy, collects our desired `path1` for both inputs, followed by a greedy `.+` that'd continue upto the last slash, and our `path2` and `path3` are in this group: `(.+)`, and our desired output can be called using `1円2円`.
####*Escaping might be unnecessary, just following based on the [demo](https://regex101.com/r/yZfVoB/1/)*.
---------
###Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^(.+?\\/).+\\/(.+)$";
final String string = "path1/path2/../path3\n"
+ "path1/./path2";
final String subst = "1ドル2ドル";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
###Demo
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log(`path1/path2/../path3
path1/./path2`.replace(/^(.+?\/).+\/(.+)$/gm, `1ドル2ドル`));
<!-- end snippet -->
###Performance
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = '/^(.+?/).+/(.+)$/gm';
const str = `path1/path2/../path3`;
const subst = `1ドル2ドル`;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");
<!-- end snippet -->
### RegEx Circuit
[jex.im](https://jex.im/regulex/#!flags=&re=%5E(a%7Cb)*%3F%24) visualizes regular expressions:
[![enter image description here][1]][1]
[1]: https://i.sstatic.net/FB7Xx.png