1

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 Answers 2

2

Use this regex instead:

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

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

See it on rubular.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm weird, it should work, I donno why it dropped the first letter in the basename: hef, ucumber, gem, it etc.
@ajsie It shouldn't (see link). Where's the rest of your code?
@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?
@ajsie Regex updated, though now I am not as confident on it. You might want to throw some test cases at it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.