Dan Rosenstark
Dan Rosenstark

Reputation: 69757

Regex: Get Filename Without Extension in One Shot?

I want to get just the filename using regex, so I've been trying simple things like

([^\.]*)

which of course work only if the filename has one extension. But if it is adfadsfads.blah.txt I just want adfadsfads.blah. How can I do this with regex?

In regards to David's question, 'why would you use regex' for this, the answer is, 'for fun.' In fact, the code I'm using is simple

length_of_ext = File.extname(filename).length
filename = filename[0,(filename.length-length_of_ext)]

but I like to learn regex whenever possible because it always comes up at Geek cocktail parties.

Upvotes: 24

Views: 54367

Answers (10)

Ariadna
Ariadna

Reputation: 1

This RegExp works for me:

(.+(?=\..+$))|(.+[^\.])

Results (bold means match):

  • test.txt
  • test 234!.something123
  • .test
  • .test.txt
  • test.test2.txt
  • .

Upvotes: 0

Aquaphor
Aquaphor

Reputation: 158

Try

(?<=[\\\w\d-:]*\\)([\w\d-:]*)(?=\.[\.\w\d-:]*)

Captures just the filename of any kind within an entire filepath. Purposefully excludes the file path and the file extension

Etc:

C:\Log\test\bin\fee105d1-5008-410c-be39-883e5e40a33d.pdf
  • Doesn't capture (C:\Log\test\bin)
  • Captures (fee105d1-5008-410c-be39-883e5e40a33d)
  • Doesn't capture (.pdf)

Upvotes: 0

DarmaniLink
DarmaniLink

Reputation: 136

I realize this question is a bit outdated, however, I had some trouble finding a good source and wound up making the regex myself. To save whoever may find this time,

If you're looking for a ~standalone~ regex

This will match the extension without the dot

\w+(?![\.\w])

This will always match the file name if it has an extention

[\w\. ]+(?=[\.])

Upvotes: 2

John Feminella
John Feminella

Reputation: 311536

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.

Upvotes: 56

Michiel d&#39;Hont
Michiel d&#39;Hont

Reputation: 336

Just the name of the file, without path and suffix.

^.*[\\|\/](.+?)\.[^\.]+$

Upvotes: 1

mortalis
mortalis

Reputation: 2151

I used this pattern for simple search:

^\s*[^\.\W]+$

for this text:

file.ext
   fileext

   file.ext.ext
 file.ext
fileext

It finds fileext in the second and last lines.
I applied it in a text tree view of a folder (with spaces as indents).

Upvotes: 0

user2120014
user2120014

Reputation: 31

^(.*)\\(.*)(\..*)$
  1. Gets the Path without the last \
  2. The file without extension
  3. 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

Upvotes: 3

sfossen
sfossen

Reputation: 4778

how about 2 captures one for the end and one for the filename.

eg.

(.+?)(?:\.[^\.]*$|$)

Upvotes: 3

David Pokluda
David Pokluda

Reputation: 10971

Ok, I am not sure why I would use regular expression for this. If I know for example that the string is a full filepath, then I would use another API to get the file name. Regular expressions are very powerfull but at the same time quite complex (you have just proved that by asking how to create such a simple regex). Somebody said: you had a problem that you decided to solve it using regular expressions. Now you have two problems.

Think again. If you are on .NET platform for example, then take a look at System.IO.Path class.

Upvotes: 0

Rex M
Rex M

Reputation: 144122

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:

(.+?)(\.[^\.]+$|$)

Upvotes: 4

Related Questions