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 you foo.bar.
  • Handles files with no dot: secret-letter gets you secret-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.

Leave a Comment