Reputation: 1306
I have this following string:
message_id = "[email protected]"
I would like to extract the last part between - .* @
which is 1661496224
With forward and backward lookup, it starts from first matches of -
but I want to match from last match of -
:
#!/bin/ruby
message_id = "[email protected]"
message_id.match(/(?<=\-).*(?=\@)
output:
#<MatchData "f97a-47b3-b42c-40868d2cef5b-1661496224">
How to capture the least match (1661496224
) between two characters?
Upvotes: 1
Views: 328
Reputation: 163372
Assuming that the message_id does not contain spaces, you might use:
(?<=-)[^-@\s]+(?=@)
If there can not be any more @ chars or hyphens after the @ till the end of the string, you can add that to the assertion.
(?<=-)[^-@\s]+(?=@[^\s@-]*$)
Another option with a capture group:
-([^-@\s]+)@[^-@\s]*$
Upvotes: 2
Reputation: 75870
Inspired by this post on SO; what about:
s = "[email protected]"
puts s[/-([^-@]*)@/,1] #assuming a single '@', otherwise:
puts s.scan(/-([^-@]*)@/).last.first
Both print:
1661496224
Upvotes: 1
Reputation: 106758
You can match all the non-dash characters that are followed by a @
:
[^-]+(?=@)
Demo: https://ideone.com/bnDxd2
Upvotes: 1