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.
Try this:
This will:
.logsis a file named.logs, not a file extension), which is common in Unix.foo.bar.jpeggets youfoo.bar.secret-lettergets 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.