'Remove backslash before forward slash

Context: GoogleBooks API returing unexpected thumbnail url

Ok so i found the reason for the problem i had in that question

what i found was the returned url from the googlebooks api was something like this:

http:/\/books.google.com\/books\/content?id=0DwKEBD5ZBUC&printsec=frontcover&img=1&zoom=5&source=gbs_api

Going to that url would return a error, but if i replaced the "\ /"s with "/" it would return the proper url

is there something like a java/kotlin regex that would change this http:/\/books.google.com\/ to this http://books.google.com/ (i know a bit of regex in python but I'm clueless in java/kotlin)

thank you



Solution 1:[1]

You can use triple-quoted string literals (that act as raw string literals where backslashes are treated as literal chars and not part of string escape sequences) + kotlin.text.replace:

val text = """http:/\/books.google.com\/books\/content?id=0DwKEBD5ZBUC&printsec=frontcover&img=1&zoom=5&source=gbs_api"""
print(text.replace("""\/""", "/"))

Output:

http://books.google.com/books/content?id=0DwKEBD5ZBUC&printsec=frontcover&img=1&zoom=5&source=gbs_api

See the Kotlin demo.

NOTE: you will need to double the backslashes in the regular string literal:

print(text.replace("\\/", "/"))

If you need to use this "backslash + slash" pattern in a regex you will need 2 backslashes in the triple-quoted string literal and 4 backslashes in a regular string literal:

print(text.replace("""\\/""".toRegex(), "/"))
print(text.replace("\\\\/".toRegex(), "/"))

NOTE: There is no need to escape / forward slash in a Kotlin regex declaration as it is not a special regex metacharacter and Kotlin regexps are defined with string literals, not regex literals, and thus do not need regex delimiters (/ is often used as a regex delimiter char in environments that support this notation).

Solution 2:[2]

You could match the protocol, and then replace the backslash followed by a forward slash by a forward slash only

https?:\\?/\\?/\S+

Pattern in Java

String regex = "https?:\\\\?/\\\\?/\\S+";

Java demo | regex demo

For example in Java:

String regex = "https?:\\\\?/\\\\?/\\S+"; 
String string = "http:/\\/books.google.com\\/books\\/content?id=0DwKEBD5ZBUC&printsec=frontcover&img=1&zoom=5&source=gbs_api";
  if(string.matches(regex)) {
    System.out.println(string.replace("\\/", "/"));
  }
}

Output

http://books.google.com/books/content?id=0DwKEBD5ZBUC&printsec=frontcover&img=1&zoom=5&source=gbs_api

Solution 3:[3]

I had same problem and my url was:

String url="https:\\/\\/www.dailymotion.com\\/cdn\\/H264-320x240\\/video\\/x83iqpl.mp4?sec=zaJEh8Q2ahOorzbKJTOI7b5FX3QT8OXSbnjpCAnNyUWNHl1kqXq0D9F8iLMFJ0ocg120B-dMbEE5kDQJN4hYIA";

I solved it with this code:

 replace("\\/", "/");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew
Solution 2 The fourth bird
Solution 3 Pir Fahim Shah