如何从正则表达式返回字符串前缀
我有一组字符串,我试图删除最后一次出现正则表达式匹配之后的所有内容。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一步完成,无需使用
split
。In one step without using
split
.试试这个正则表达式:
代码:
输出:
Rubular链接:http://rubular.com/r/kKrseNE7ZX
Try this regular expression:
Code:
Output:
Rubular link: http://rubular.com/r/kKrseNE7ZX