4

My goal is to replace all "/" and "\" with one "\".

Input:

String path = "H\\\\\\\\\\ello///Wo\\rld\\!////";

Output:

String path = "H\ello\Wo\rld\!\";

Question:

Is there a way to do that all in one single line?

My solution (working!):

  1. change all to the same char
  2. replace all "/" with one Slash
  3. put it back to normal

I learned that I need regex and that "[X]+" will put all possible combinations eg. "XXfXfXXXX" to "XfXfX".

I am searching for something like: (pseudocode)

path = path.replaceAll("[\\/]+", "\\");

I tried some combinations of that but everytime it throw errors.

My Solution:

path = path.replace("\\", "/");
path = path.replaceAll("[/]+", "/");

pfad = pfad.replace("/", "\\");
10
  • I'm afraid you'll have to iterate here thru the chars one by one Commented Apr 29, 2019 at 13:27
  • Why do you want to replace \ with \? It doesn't change anything. Do you perhaps want to replace series of \ and / with single \? Commented Apr 29, 2019 at 13:29
  • Yes, that is what I am aiming for @Pshemo Commented Apr 29, 2019 at 13:30
  • 3
    Try .replaceAll("[\\\\/]+", "\\\\") if all chunks of 1 or more \ and / chars should be replaced with one \. Commented Apr 29, 2019 at 13:30
  • 1
    What should be result of replacing \\\\//? Should it be one \ or maybe two \\ (one for \\\\ sequence and second for // sequence)? Commented Apr 29, 2019 at 13:35

2 Answers 2

4

Try this pattern to match groups of slashes and backslashes: (?:\\+|\/+) and replace it with \\.

Explanation:

(?:...) - noncapturing group

\\+ - match one or more \

\/+ - match one or more /

| - alternation: match pattern on the right or on the left

Demo

Alternatively you could use pattern [\/\\]+, which matches one or more of \ or /

Another demo

Sign up to request clarification or add additional context in comments.

10 Comments

This won't work if there's a mix of back- and forward-slashes. E.g. H\\\\///\\\ello///Wo\\rld\!/\//. It might be good enough for OP though ;)
@SamWhan That's why I included alternative, which would work in your scenario :)
Simply [\\\/]+ is sufficient
@Strelok That's why I also suggested it
No need to escape / char, it is not any special in a regex.
|
3

Use

.replaceAll("[\\\\/]+", "\\\\")

See the regex graph:

enter image description here

The [\\/]+ pattern matches \ or / one or more times. The replacement pattern is \\ since a backslash in the replacement pattern is special in Java, it is used to escape the $ symbol that denotes a literal $ char. To match a literal backslash, you need to use four \ in the regex string literal.

See Java demo online:

String path = "H\\\\\\\\\\ello///Wo\\rld\\!////";
System.out.println(path.replaceAll("[\\\\/]+", "\\\\"));
// => H\ello\Wo\rld\!\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.