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

  • SEARCH
  • Home
  • 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 6660019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:06:10+00:00 2026-05-26T02:06:10+00:00

I need to get a file name from file’s absolute path (I am aware

  • 0

I need to get a file name from file’s absolute path (I am aware of the file.getName() method, but I cannot use it here).
EDIT: I cannot use file.getName() because I don’t need the file name only; I need the part of the file’s path as well (but again, not the entire absoulte path). I need the part of file’s path AFTER certain path provided.

Let’s say the file is located in the folder:

C:\Users\someUser

On windows machine, if I make a pattern string as follows:

String patternStr = "C:\\Users\\someUser\\(.*+)";

I get an exception: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence for backslash.

If I use Pattern.quote(File.pathSeparator):

String patternStr = "C:" + Pattern.quote(File.separator) + "Users" + Pattern.quote(File.separator) + "someUser" + Pattern.quote(File.separator) + "(.*+)";

the resulting pattern string is: C:\Q;\EUsers\Q;\EsomeUser\Q;\E(.*+) which of course has no match with the actual fileName “C:\Users\someUser\myFile.txt”.

What am I missing here? What is the proper way to parse file name?

  • 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-26T02:06:11+00:00Added an answer on May 26, 2026 at 2:06 am

    If you indeed want to use a regular expressions, you should use

    String patternStr = "C:\\\\Users\\\\someUser\\\\(.*+)";
                           ^^       ^^          ^^
    

    instead.

    Why? Your string literal

    "C:\\Users\\someUser\\(.*+)"
    

    is compiled to

    C:\Users\someUser\(.*+)
    

    Since \ is used for escaping in regular expressions too, you’ll have to escape them “twice”.


    Regarding your edit:

    You probably want to have a look at URI.relativize(). Example:

    File base = new File("C:/Users/someUser");
    File file = new File("C:/Users/someUser/someDir/someFile.txt");
    
    String relativePath = base.toURI().relativize(file.toURI()).getPath();
    
    System.out.println(relativePath); // prints "someDir/someFile.txt"
    

    (Note that / works as file-separator on Windows machines too.)


    Btw, I don’t know what you have as File.separator on your system, but if it’s set to \, then

    "C:" + Pattern.quote(File.separator) + "Users" + Pattern.quote(File.separator) +
        "someUser" + Pattern.quote(File.separator) + "(.*+)";
    

    should yield

    C:\Q\\EUsers\Q\\EsomeUser\Q\\E(.*+)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the name of a file from its full path, in
I need a little help here: I get a file from an HTML upload
my window.location is F:/html5/home.html, from my location i need to get the file name
Need to get the css file name from a link tag that is in
I need to get a file name from a directory for the file to
I need to get some external data to form an output file name in
In C#, Whenever i need to get file path more dyanmically, i do something
I need to get the File version information from an exe file originally written
I need to get a file from sourcesafe database programmatically. Any idea of how
I've need to get a file from a remote (embedded) system (which I can

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.