Reputation: 10541
Here's my file:
name.extension
And here's my regex:
.*[.]
However this matches the filename and the period:
#=> "filename."
How can I exclude the period in order to achieve:
#=> "filename"
I'm using Ruby.
Upvotes: 0
Views: 197
Reputation: 13901
Alternatively, you can create subgroups in the regexp and just select the first:
str = 'name.extension'
p str[/(.*)[.]/,1] #=> name
Upvotes: 0
Reputation: 8003
Match upto the last "."
"filen.ame.extension"[/.*(?=\.)/]
# => filen.ame
Match upto first "."
"filen.ame.extension"[/.*?(?=\.)/]
# => filen
Upvotes: 0
Reputation: 118261
You can use File
class methods File#basename
and File#extname
:
file= "ruby.rb"
File.basename(file,File.extname(file))
# => "ruby"
Upvotes: 3
Reputation: 213223
You just need a negated character clas:
^[^.]*
This will match everything, from the beginning of the string till it finds a period (but not include it).
Upvotes: 1