title: How can I remove all trailing backslashes from a string in Scala?
body: <p>I want to remove all trailing backslashes (<code>'\'</code>) from a string.</p>
  
  <p>For example:</p>
  
  <pre><code>"ab"       -&gt; "ab" 
  "ab\\\\"   -&gt; "ab"
  "\\\\ab\\" -&gt; "\\\\ab"
  "\\"       -&gt; ""
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>val str = """\\\\q\\"""
  val regex = """^(.*[^\\])(\\+)$""".r
  str match  {
    case regex(rest, slashes) =&gt; str.stripSuffix(slashes)
    case _ =&gt; str
  }
  </code></pre>