Pattern p = Pattern.compile("(?<!/)/(//)*(?!/)");
Matcher m = p.matcher(inputString);
String outputStr = m.replaceAll("$0$0");
(?<!/)
makes sure there are no slashes right before the match;/(//)*
matches an odd number of slashes;(?!/)
makes sure there are no slashes right after the match.
The replacement string is $0$0
, which doubles up the matched slashes.
I’ve tested this on your inputs, and it works exactly as per your spec.
1
solved Regular Expressions in Java replace odd # of slashes