tags:

views:

31

answers:

2

I have this list:

foo chef.rb baz
bar cucumber.rb bar
baz gem.rb foo

I want to capture all the names without .rb.

My current regexp looks like this:

/([^\s](?:.)*?.(?:rb))/i

But it captures the .rb too.

How do I capture just the base name?

Thanks.

+2  A: 

Use this regex instead:

/(\w*?)\.rb\s*.*/i

And your base-name will be in the 1st capture group.

See it on rubular.

NullUserException
`?` will fail on "file.rblol.rb". Should be `/(.*)\.rb/i`
Nakilon
Hmm weird, it should work, I donno why it dropped the first letter in the basename: hef, ucumber, gem, it etc.
never_had_a_name
@Nakilon Fixed that
NullUserException
@ajsie It shouldn't (see link). Where's the rest of your code?
NullUserException
@NullUserException, ok, your fix also works )
Nakilon
@NullUserException: Look at my update. I forgot to tell you there are letters before and after the filenames. Could you customize your regexp to that?
never_had_a_name
@ajsie Regex updated, though now I am not as confident on it. You might want to throw some test cases at it.
NullUserException
A: 

This is a bit simpler: /(\S+).rb(?:$|\s)/

Any non-space chars followed by .rb followed by either the end of line or a space.

glenn jackman