I have a string like so:
"@[30:Larry Middleton]"
I want to return just 30. Where 30 will always be digits, and can be of 1 to infinity in length.
I’ve tried:
user_id = result.match(/@\[(\d+):.*]/)
But that returns everything. How can I get back just 30?
If that’s really all your string, you don’t need to match the rest of the pattern; just match the consecutive integers:
However, if you need to match this as part of a larger string that might have digits elsewhere:
If you need the name also:
In Ruby 1.9 you can even name the matches, instead of using the capture number:
And if you need to find many of these: