Regex: Get Filename Without Extension in One Shot?
Try this:
(.+?)(\.[^.]*$|$)
This will:
- Capture filenames that start with a dot (e.g.
.logs
is a file named.logs
, not a file extension), which is common in Unix. - Gets everything but the last dot:
foo.bar.jpeg
gets youfoo.bar
. - Handles files with no dot:
secret-letter
gets yousecret-letter
.
Note: as commenter j_random_hacker suggested, this performs as advertised, but you might want to precede things with an anchor for readability purposes.
Everything followed by a dot followed by one or more characters that's not a dot, followed by the end-of-string:
(.+?)\.[^\.]+$
The everything-before-the-last-dot is grouped for easy retrieval.
If you aren't 100% sure every file will have an extension, try:
(.+?)(\.[^\.]+$|$)
^(.*)\\(.*)(\..*)$
- Gets the Path without the last
\
- The file without extension
- The the extension with a
.
Examples:
c:\1\2\3\Books.accdb
(c:\1\2\3)(Books)(.accdb)
Does not support multiple .
in file name
Does support .
in file path
how about 2 captures one for the end and one for the filename.
eg.
(.+?)(?:\.[^\.]*$|$)