This question already has answers here:
How to extract URL parameters from a URL with Ruby or Rails?
(10 answers)
Closed 7 years ago.
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?
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"
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"]]
Related
This question already has answers here:
Double vs single quotes
(7 answers)
Closed 2 years ago.
I am newbie in Ruby.
Are there some differences between "string" and 'string' in ruby?
such as
txt_data.gsub("ABC",'')
and
txt_data.gsub('ABC','')
It seems they are always the same.
Are there some cases where we need to use "" and '' selectively?
The difference is, you could interpolate the value when you use "" but you can't when you use ''
See the below example
value=23
a="The value is #{value}"
p a
a='The value is #{value}'
p a
Output
"The value is 23"
"The value is \#{value}"
This question already has answers here:
Ruby global match regexp?
(3 answers)
Closed 5 years ago.
I got a string "[5, 3]" and I want to extract 5 and 3 from it using regex.
p "[5, 3]".match(/\d,\s\d/) # <MatchData "5, 3">
p "[5, 3]".match(/\d/) # <MatchData "5">
I can't extract both numbers at same time
I think you need scan instead match, try:
p "[5, 3]".scan(/\d/)
What I can say is match returns the first match as MatchData, while scan returns everything what the regex matches.
See String#scan and Regexp#match.
This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Ruby backslash to continue string on a new line?
(3 answers)
Closed 8 years ago.
If I have this code:
a = "hi" "pie"
puts a
It will print out hipie. Does Ruby automatically combine these?
Yes. From Literals: String
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion"
#=> "concatenation"
"This string contains " "no newlines."
#=> "This string contains no newlines."
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the colon operator in Ruby?
While learning Ruby I've come across the ":" operator on occasion. Usually I see it in the form of
:symbol => value
what does it mean?
It just indicates a that it is a symbol instead of a string. In ruby, it is common to use symbols instead of strings.
{:foo => value}
{'foo' => value}
It's basically a short-hand way of expressing a string. It can not contain spaces as you can imagine so symbols usually use underscores.
Try this on your own:
foo = :bar
foo.to_s # means to string
baz = 'goo'
baz.to_sym # means to symbol
This question already has answers here:
Validate that string contains only allowed characters in Ruby
(4 answers)
Closed 2 years ago.
Given a string of a mobile phone number, I need to make sure that the given string only contains digits 0-9, (,),+,-,x, and space. How can I do it in Ruby?
Use:
/^[-0-9()+x ]+$/
E.g.:
re = /^[-0-9()+x ]+$/
match = re.match("555-555-5555")
if (/^[-\d()\+x ]+$/.match(variable))
puts "MATCH"
else
puts "Does not MATCH"
end
Use String#count:
"+1 (800) 123-4567".count("^0-9+x()\\- ").zero? # => true
"x invalid string x".count("^0-9+x()\\- ").zero? # => false