JP
/
EN

Regex: Filename & Directory

SummaryWindowsUnix
File name[^\\]+$[^/]+$
Directories^.*\\^.*/
Since a specific directorydirectory.*$directory.*$
After a specific directory(?<=directory\\).*$(?<=directory/).*$
Until a specific directory^.*directory\\^.*directory/
Before a specific directory.*\\(?=directory).*/(?=directory)
* The difference between "Since/Until" and "After/Before" is whether the specified directory name is included or not.

This is a regular expression that matches part of a file path. Only file names, only directories, only after a specific directory, etc.

These are useful when processing text such as a list of full paths to files; note that Windows and Unix-like systems use different delimiters, so the regular expressions will be different.

Regex Usage Examples

Suppose you have the following text.

C:\Users\regex\Documents\foo.txt
C:\Users\regex\Documents\bar\baz.txt
C:\Users\regex\Documents\qux\qux\qux.txt

Removing filename from path

You can extract only directory paths by using [^\]+$ to search only for file names and replacing them with empty characters.

C:\Users\regex\Documents\
C:\Users\regex\Documents\bar\
C:\Users\regex\Documents\qux\qux\

Removing directory from path

You can extract only file names by using ^.*\\ to search only for directory paths and replacing them with empty characters.

foo.txt
baz.txt
qux.txt

Removing only before a specific directory from path

Search up to a specific directory with ^.*Documents\\ and replace with an empty string to extract only the paths after the specific directory.

foo.txt
bar\baz.txt
qux\qux\qux.txt
Hirota Yano / Japan / Programmer
I am publishing a web tool I created as a hobby. It is free of charge, so please feel free to use it.
© Hirota Yano