Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:37:30+00:00 2026-05-28T05:37:30+00:00

Considering a path such as var dir = new File(c:\test\project1); How do I easily

  • 0

Considering a path such as

var dir = new File(“””c:\test\project1″””);

How do I easily in Scala escape/quote this so it can be used safely in regular expressions

val extractRelativePath = (dir.getAbsolutePath() + “””(.*)”””).r

I tried using

dir.getAbsolutePath().replaceAll(“\\”, “\\\\”);

but this does not work. as the following example shows

def main(args: Array[String]): Unit = {
  var base = new File("""c:\test\project1""");
  val extractRelativePath = (base.getAbsolutePath() + """(.*)""").r

  var dir = new File("""c:\test\project1\somedir""");
  var extractRelativePath(rel) = dir.getAbsolutePath().replaceAll("\\\\", "\\\\")
}

Also is there not some standard functionality that does this safely across platforms like Pattern.quote ?.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:37:31+00:00Added an answer on May 28, 2026 at 5:37 am

    You can use quotations, but I think that in your case you also need to escape \E. Following code should do it:

    ("""\Q""" + base.getAbsolutePath.replaceAll("\\\\E", "\\\\E\\\\\\\\E\\\\Q") + """\E(.*)""").r
    

    I generally replacing \E with \E\\E\Q, so I split quotation and explicitly adding \\ followed by E in regexp.

    Here is small example. If I have defined base like his:

    var base = new File("""c:\test\Earth""");
    

    then it will produce following regexp:

    \Qc:\test\E\\E\Qarth\E(.*)
    

    As an advantage to this approach, \Q and \E will escape everything and not only * or \.


    Here is the whole example code:

    var base = new File("""c:\test\Earth""");
    val extractRelativePath = ("""\Q""" + base.getAbsolutePath.replaceAll("\\\\E", "\\\\E\\\\\\\\E\\\\Q") + """\E(.*)""").r
    
    var dir = new File("""c:\test\Earth\somedir""");
    var extractRelativePath(rel) = dir.getAbsolutePath
    
    println(rel) // prints: \somedir 
    

    By the way

    You can also use Pattern.quote which makes exactly the same, but more efficiently:

    (Pattern.quote(base.getAbsolutePath) + """(.*)""").r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering this code, can I be absolutely sure that the finally block always executes,
For parallel processing of graphs such as shortest path computation, would Scala outperform C++?
I need to glob ./../path/to/files/**/*.txt but instead of receiving matches like this: ./../path/to/files/subdir/file.txt I
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
I am considering the StringTemplate view engine for my ASP.NET MVC application. This application
This is the code I'm using (it's the index.php file) /* Set an include
The quick overview is this: for my web app I can write most of
If you follow some of MS's recommended branching strategies you can easily end up
Say I have a text file of 1 MB. Considering the following the example:
This one has been driving me nuts... Considering plane is a CALayer, I rotate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.