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?
Try this regular expression:
Code:
Output:
Rubular link: http://rubular.com/r/kKrseNE7ZX