I'm receiving urls in the following format:
http://www.whatever.com/maybesomthinghere/2323923723?what=what&who=no
I'd like some regex to grab "2323923723" from the URL. It may or may not have query string elements appended at the end.
You can always use str.delete like this:
num = str.delete("^0-9")
Output:
[7] pry(main)> str
=> "http://www.whatever.com/maybesomthinghere/2323923723?what=what&who=no\n"
[8] pry(main)> str.delete("^0-9")
=> "2323923723"
->() { } only.. :-)If you simply want to capture any sequence of numbers out of the url, then this should work:
s = [url]
/[0-9]+/.match(s)
If you want to find a sequence of numbers bounded by / and ?, then you'd want something more like:
s = [url]
/\/([0-9]+)\?/.match(s)
You could be much more specific if you need to verify that you are working with a valid URL, if the numbers need to be bounded by more specific text, etc.
You may want to use rubular (http://rubular.com/) to test interactively.
Use this expression since the URL might contain other numbers after the ? (within the query string) or even before.
/(\d+)(?:\?)/