How can I remove all trailing backslashes from a string in Scala?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I remove all trailing backslashes from a string in Scala?



I want to remove all trailing backslashes ('') from a string.


''



For example:


"ab" -> "ab"
"ab\\" -> "ab"
"\\ab\" -> "\\ab"
"\" -> ""



I am able to do this using below code but unable to handle the scenario where the String has only slash(es). Please let me know if this can be achieved through a different regex.


val str = """\\q\"""
val regex = """^(.*[^\])(\+)$""".r
str match
case regex(rest, slashes) => str.stripSuffix(slashes)
case _ => str





Try: str.replaceFirst("\\+$", "");
– anubhava
Aug 10 at 15:07


str.replaceFirst("\\+$", "");





Maybe add this as an answer ? Seems great :-)
– C4stor
Aug 10 at 16:06





@anubhava, it works. Guess you don't need any more rep:)
– sujit
Aug 10 at 17:55





Better use str.replaceFirst("""\+$""", "");, no need overescaping.
– Wiktor Stribiżew
Aug 10 at 19:55



str.replaceFirst("""\+$""", "");




5 Answers
5



Converting my comment as an answer. This should work for removing all trailing backslashes:


str = str.replaceFirst("\\+$", "");



\\+ matches 1+ backslashes (single backslash is entered as \\ in Java/Scala).


\\+


\\



While not a regex, I suggest a simpler solution : str.reverse.dropWhile(_ == '\').reverse


str.reverse.dropWhile(_ == '\').reverse





Unclosed literal character... Backslashes! Backslashes! That's why it's so fun! ;)
– Andrey Tyukin
Aug 10 at 15:31




Not using a regex, but you could use String.lastIndexWhere(p: (Char) ⇒ Boolean) to get the position of the last character which is not a '' in order to substring until this character:


String.lastIndexWhere(p: (Char) ⇒ Boolean)


''


str.substring(0, str.lastIndexWhere(_ != '\') + 1)



If, for some reason, you're committed to a regex solution, it can be done.


val regex = """[^\]?(\*)$""".r.unanchored
str match
case regex(slashes) => str.stripSuffix(slashes)





seems i was complicating the solution to a simple question, but thanks for bending it my way. Can i ask why """\+$""".r.unachored when replaced in your answer doesn't work?
– sujit
Aug 10 at 18:17


"""\+$""".r.unachored





The + means "one-or-more" so a string without a backslash won't match. The *, zero-or-more, will always make a capture. If there are no backslashes then the capture is empty, but still valid.
– jwvh
Aug 10 at 18:27


+


*



You can do the same with slice function


slice


str.slice(0,str.lastIndexWhere(_ != '\')+1)





it works. not sure why someone downvoted.
– sujit
Aug 10 at 17:56





Yes, He should comment.
– Manoj Kumar Dhakd
Aug 10 at 18:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard