Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Right now I'm scrapping this:
(178,979)
with this:
.text[/[\d,]+/]
to turn it into this:
178,979
Now I want to scrap this:
Showing 1 - 24 of 512,518 Results
and turn it into this:
512,518
What's the easiest way of accomplishing that?
I'd go about it something like this:
'Showing 1 - 24 of 512,518 Results'[/\d+,\d+/] # => "512,518"
Or this:
'Showing 1 - 24 of 512,518 Results'[/(\S+) Results$/i, 1] # => "512,518"
str = 'Showing 1 - 24 of 512,518 Results'
str.scan(/[\d,]+/)[2]
#=> "512,518"
str[/\d+,\d+/]
#=> "512,518"
You are looking for an integer, followed by a comma, again followed by an integer. So this regex should do the trick:
/\d+,\d+/
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Rearrange the key values in the hash below to look like:
{"abc"=>"wqeq","dfg"=>"sadsada","qwe"=>"asdad","yui"=>"asdasd","abc"=>"weqqw","qwe"=>"assadsad","yui"=>"asd","dfg"=>"asdsad"}
{"abc"=>["wqeq","weqqw"] ...}
Thanks
The problem here is that assigning a key with the same value as a previous one overwrites it.
As far as I'm aware, there's no code-based solution to solve this; you just need to re-write it with arrays as the keys' values where they're duplicated.
{"abc" => ["wqeq", "weqqw"],
"dfg" => ["sadsada", "asdsad"],
"qwe" => ["asdad", "assadsad"],
"yui" => ["asdasd", "asd"] }
I guess that's the output you're looking for. If not, please add a little more info to the questions and I / others will be able to help out.
If you want to bring a gun to a knife fight, you can use compare_by_identity:
hash = {}
hash.compare_by_identity
hash["abc"] = "wqeq"
hash["abc".dup] = "weqqw"
# ... etc ...
puts hash # => { "abc" => "wqeq", "abc" => "weqqw" }
... and then process that. Feels kinda dirty to me though.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What would be the right way to find the location of a character within the alphabet? For example:
"A".find_score # => 1
"C".find_score # => 3
"A".ord
returns 65, the numeric code for "A", which is what the alphabet starts at. If you want it to start at 1 you could just subtract 64:
def get_code(c)
c.upcase.ord - 'A'.ord + 1
end
which works like:
get_code('A') # 1
get_code('B') # 2
get_code('C') # 3
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need a regular expression that only matches three digit numbers in the following array. I need the result to be a new array.
Input:
my_array = [111,45456,456,74897,787,45466,789,6587,784,234,456,4658,4587,235,456]
Desired output:
new_array = [111,456,787,789,784,234,456,235,456]
Why regular expression on numbers? You can select all numbers less than 1000 and greater than 99.
my_array.select { |n| n<1000 && n>99 }
Just the regexp would look like this: /^\d{3}$/. But if you'd like an expression that would return an array of values that match that expression this would do it: my_array.select{ |num| num.to_s.match(/^\d{3}$/) }.
Take a look at RegExr to learn more about Regular Expressions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)