Get 2 values from string using regex [duplicate] - ruby

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.

Related

Ruby - Extract data from string with regex [duplicate]

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"]]

Why does ruby automatically combine Strings? [duplicate]

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."

Ruby: What does the "!~" operator mean? [duplicate]

This question already has answers here:
What does the !~ method do with String in Ruby
(2 answers)
Closed 8 years ago.
When declaring syntax such as:
a !~ b
where a,b are variables, what does it mean?
It is negation of =~, a regex match.
"a" !~ /b/
# => true
It is useful when you want to check whether a string does not match a certain pattern. For example, if you want to check if string s includes only numbers, then you can do:
s !~ /\D/

Capitalize, swapcase and reverse in Ruby [duplicate]

This question already has answers here:
Ruby modify a piece of a string
(5 answers)
Closed 9 years ago.
I have a challenging Ruby problem:
I want to convert "howdy" to "YDWOh" and "how are you" to "uoy era WOh".
which is essentially capitalize, swapcase and reverse functions. Capitalize and swapcase is only for the first word of the sentence. How do I do this when I have more than one word in the sentence?
Try the following steps:
capitalize your string
split(' ') your string into an array with words
Use each_with_index.map to go over each word in the array
Use swapcase on only the first word of the array
join(' ') the array back into a string
reverse the string
You can try the below:
a = "how are you"
p a.gsub(a.split[0...1].join(' '),a.split[0...1].join(' ').capitalize.swapcase).reverse
Output:
"uoy era WOh"

How can I test whether a string only contains characters in a given set? [duplicate]

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

Resources