如何从正则表达式返回字符串前缀

发布于 2024-11-09 10:33:56 字数 593 浏览 0 评论 0原文

我有一组字符串,我试图删除最后一次出现正则表达式匹配之后的所有内容。

Sample Data
23401BK221
23430-BZ-221
1004113-BK-3
14989r-113
30402113

我试图这样做,

extensions_to_remove="BK|BZ|113"
sample_data = sample_data.split(/.*(#{extensions_to_remove}$1)/)

我希望我能得到一个数组,我可以在其中获取第一个条目,但不幸的是,我得到的

["","BK", "-221"]
["","BZ","-221"]
["","BK", "-3"]
["","113"]
["", "113"]

是我希望得到的是

23401
23430
1004113
14989r
30402

所以基本上删除了最后一场比赛后的所有内容,并且那么如果它们是尾随的“-”,我会尝试将其删除。

我想如果我将它放入数组中,我可以获取第一个值,然后删除尾随的“-”(如果存在)。

关于我做错了什么有什么建议吗?为什么我没有取回前缀? 有更好的方法吗?

I've got a group of strings and I'm trying to remove everything after and including the last occurance of a regexp match.

Sample Data
23401BK221
23430-BZ-221
1004113-BK-3
14989r-113
30402113

I am attempting to do this with

extensions_to_remove="BK|BZ|113"
sample_data = sample_data.split(/.*(#{extensions_to_remove}$1)/)

I was hoping that I would get an array where I could just take the first entry, but unfortunately, I'm getting

["","BK", "-221"]
["","BZ","-221"]
["","BK", "-3"]
["","113"]
["", "113"]

What I'm hoping to get is

23401
23430
1004113
14989r
30402

So essentially remove everthing after the last match, and then if their is a trailing '-' I'm trying to remove that.

I figured if I got it into an array, I could take the first value, then strip the trailing '-', if it existed.

Any suggestions on what I'm doing wrong? Why I'm not getting the prefixes back?
Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-11-16 10:33:56

一步完成,无需使用 split

sample_data = %w[
  23401BK221
  23430-BZ-221
  1004113-BK-3
  14989r-113
  30402113
]
sample_data = sample_data.map{|s| s[/(.*[^-])-?(?:BK|BZ|113)/, 1]}

In one step without using split.

sample_data = %w[
  23401BK221
  23430-BZ-221
  1004113-BK-3
  14989r-113
  30402113
]
sample_data = sample_data.map{|s| s[/(.*[^-])-?(?:BK|BZ|113)/, 1]}
完美的未来在梦里 2024-11-16 10:33:56

试试这个正则表达式:

(\w+).*(BK|BZ|113)

代码:

data = ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"]
data.each {|d| p d.split(/(\w+).*(BK|BZ|113)/)[1]}

输出:

gazler@gazler-desktop:~$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
gazler@gazler-desktop:~$ irb
ruby-1.9.2-p180 :001 > data = ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"]
 => ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"] 
ruby-1.9.2-p180 :002 > data.each {|d| p d.split(/(\w+).*(BK|BZ|113)/)[1]}
"23401"
"23430"
"1004113"
"14989r"
"30402"

Rubular链接:http://rubular.com/r/kKrseNE7ZX

Try this regular expression:

(\w+).*(BK|BZ|113)

Code:

data = ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"]
data.each {|d| p d.split(/(\w+).*(BK|BZ|113)/)[1]}

Output:

gazler@gazler-desktop:~$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
gazler@gazler-desktop:~$ irb
ruby-1.9.2-p180 :001 > data = ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"]
 => ["23401BK221", "23430-BZ-221", "1004113-BK-3", "14989r-113", "30402113"] 
ruby-1.9.2-p180 :002 > data.each {|d| p d.split(/(\w+).*(BK|BZ|113)/)[1]}
"23401"
"23430"
"1004113"
"14989r"
"30402"

Rubular link: http://rubular.com/r/kKrseNE7ZX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文