-5

In ruby I have string that looks like this:

"\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"

how can I extract offset value from this string with regular expressions?

0

2 Answers 2

3

It doesn't satisfy your requirement to use a regex, but here is a way:

uri = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"

require "uri"
URI.decode_www_form(URI(uri).query).assoc("offset").last
# => "20"

or

URI.decode_www_form(URI(uri).query).to_h["offset"]
# => "20"
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming offset will always be present as offset= and it will always be a numeric value

str = "\/v1\/195900\/patients?DEPARTMENTID=162&GUARANTORCOUNTRYCODE3166=1&offset=20"
str.scan(/offset=(\d+)/)
#=> [["20"]]

1 Comment

You can use a positive lookbehind to avoid the nested array: str.scan(/(?<=offset=)\d+/), or - since there is probably only one "offset" - just str[/(?<=offset=)\d+/]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.