Reputation: 69757
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
Reputation: 1
This RegExp works for me:
(.+(?=\..+$))|(.+[^\.])
Results (bold means match):
Upvotes: 0
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
Upvotes: 0
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
Reputation: 311536
Try this:
(.+?)(\.[^.]*$|$)
This will:
.logs
is a file named .logs
, not a file extension), which is common in Unix.foo.bar.jpeg
gets you foo.bar
.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
Reputation: 336
Just the name of the file, without path and suffix.
^.*[\\|\/](.+?)\.[^\.]+$
Upvotes: 1
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
Reputation: 31
^(.*)\\(.*)(\..*)$
\
.
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
Reputation: 4778
how about 2 captures one for the end and one for the filename.
eg.
(.+?)(?:\.[^\.]*$|$)
Upvotes: 3
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
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