Skip to main content
Code Review

Return to Revisions

2 of 3
added 259 characters in body
Emma Marcier
  • 3.7k
  • 3
  • 14
  • 43

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 .


###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

console.log(`path1/path2/../path3
path1/./path2`.replace(/^(.+?\/).+\/(.+)$/gm, `1ドル2ドル`));

###Performance

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. 😳 ");

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma Marcier
  • 3.7k
  • 3
  • 14
  • 43
default

AltStyle によって変換されたページ (->オリジナル) /