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("/", "\\");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
xentus
  • 41
  • 1
  • 3
  • I'm afraid you'll have to iterate here thru the chars one by one – Naya Apr 29 '19 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 ``\``? – Pshemo Apr 29 '19 at 13:29
  • Yes, that is what I am aiming for @Pshemo – xentus Apr 29 '19 at 13:30
  • 3
    Try `.replaceAll("[\\\\/]+", "\\\\")` if all chunks of 1 or more ``\`` and `/` chars should be replaced with one ``\``. – Wiktor Stribiżew Apr 29 '19 at 13:30
  • Like I said, my solution works with that 3 steps, just thinking about if there s a way to put it in one replaceAll() – xentus Apr 29 '19 at 13:31
  • 1
    What should be result of replacing ``\\\\//``? Should it be one ``\`` or maybe two ``\\`` (one for ``\\\\`` sequence and second for `//` sequence)? – Pshemo Apr 29 '19 at 13:35
  • Are the strings in your example not escaped? Are you manipulating source code? I'm confused since the second string is a syntax error – Ruan Mendes Apr 29 '19 at 13:56
  • My apologies if i confused you. No, I am not trying to manipulate "The Matrix" :D. It s just a short pic of a bigger problem. Of course it would be \\ in a String but I am not trying to get a "real" String as result. I missed one \ after "rld\" too – xentus Apr 29 '19 at 14:36
  • See https://ideone.com/xhUr8C. One backslash is missing in your string literal before `!`, I added it there. – Wiktor Stribiżew Apr 29 '19 at 14:41
  • @xentus I believe my first-posted solution in the [comment](https://stackoverflow.com/questions/55904397/how-to-replace-multiple-slash-and-backslash-in-a-string-with-a-single-one/55905751#comment98464405_55904397) is your answer. If yes, please consider accepting [my answer](https://stackoverflow.com/a/55905751/3832970). – Wiktor Stribiżew Apr 30 '19 at 23:19

2 Answers2

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

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
1

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\!\
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563