开发者

Help with capturing with regexp

开发者 https://www.devze.com 2023-01-16 07:14 出处:网络
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.

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.


Use this regex instead:

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

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

See it on rubular.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消