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
I have a string pattern like below,
=> "text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_cha
rt.png"
I want to have to separate url and then string one by one like, the output should be,
> ["text1",
> "http://www.domain.com/experiment_B_setup_diagram_image.png",
> "\n\nExperiments", "text2",
> "http://www.domain.com/experiment_C_setup_diagram_image.png",
> "\nexperiment", "text3",........]
like separate url and its preceeding or succeeding strings as an array...Can somebody help me out?
Try using:
.split(/\s|\n+/)
For example:
text="text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_chart.png"
text.split(/\s|\n/)
Which returns:
["text1", "http://www.domain.com/experiment_B_setup_diagram_image.png", "", "Experiments", "text2", "http://www.domain.com/experiment_C_setup_diagram_image.png", "", "experiment", "text3", "http://www.domain.com/experiment_A_plotted_chart.png"]
As long as you're OK with dropping the line feeds (\n), you can convert them to a space first and then simply split on spaces:
x = "text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_chart.png"
x.tr("\n", " ").split(" ")
Related
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 4 years ago.
Improve this question
I have a list of IPs in a text document, I use File.open or File.readline to keep inside a string and I have to build a Regex to get the most occurred IP in the list.
This is what I have so far:
file = File.open("/Users/leonardoeiki/workspace/foo.txt", "r")
ips = Array.new
file.each_line do |line|
array = line.match("some regex that return the ips")
end
# some code that return me the most occurred ip on the
# array file = File.read("/Users/leonardoeiki/workspace/foo.txt")
There are other values in the archive like hours, alerts and errors messages, but I only need to return the most occurred ip
Based on the following text file
#file.txt
some text 192.168.1.1 some more text
some text 108.302.22.108 some more text
some text 108.302.22.108 some more text
Your ruby code should work with this
ips = File.readlines("file.txt").map do |line|
line.scan(/\d{1,}\b/).join('.')
end
ips.map{|ip| {ip: ip, count: ips.select{|i| i == ip }.count}}.max_by{|h| h[:count]}
#=> {:ip=>"108.302.22.108", :count=>2}
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
I have "8 560,90 cur.". How do I get the whole number 8560 as integer?
I can split it by comma, and then get [0] as whole number, asked if there's more way to do it.
Here's how I'd do it:
str = "8 560,90 cur."
str.gsub(/[^\d,]/, '').to_i
# => 8560
This removes every character that isn't a digit or a comma, yielding "8560,90", then calls to_i on it, which gives 8560. This will work for any string as long as you want every digit before the first comma to be part of the number, and none after.
"8 560,90 cur.".scan(/(\d*?).+?(\d+,\d\d)/).flatten.join.to_i
# => 8560
"sdwfdsf560,90 cur.".scan(/(\d*?).+?(\d+,\d\d)/).flatten.join.to_i
# => 560
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 have an array with string values:
a = ["Customer name", "Address", "Qualification"]
Requirement is to make these string values enclosed in both single and double quotes like this:
a = ["'Customer name'", "'Address'", "'Qualification'"]
How can I achieve this?
a = ["Customer name", "Address", "Qualification"]
a.map { |i| "'#{i}'" } # => ["'Customer name'", "'Address'", "'Qualification'"]
It makes sense to say that you want to enclose the content of each string with single quotes, but it does not make sense to say that you want to have double quotes around it, that is part of the literal. But anyway,
a.map{|s| "'#{s}'"}
# => ["'Customer name'", "'Address'", "'Qualification'"]
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 want to split the string from the last underscore. The string as below:
"abc_123_identifier_12345"
the output:
["abc_123_identifier", "12345"]
please tell me if you have same good ideas. Thanks in advance!
Try this:
"abc_123_identifier_12345".split(/_(\d+)$/)
#=> ["abc_123_identifier", "12345"]
a = "abc_123_identifier_12345"
a.rpartition('_') - ['_']
output in console
[22] pry > a = "abc_123_identifier_12345"
=> "abc_123_identifier_12345"
[23] pry > a.rpartition('_') - ['_']
=> ["abc_123_identifier", "12345"]
Looks more like a pattern matching task than a splitting task for me:
[1] pry(main)> /^(.*)_(\d*)$/.match("abc_123_identifier_12345").captures
=> ["abc_123_identifier", "12345"]
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 8 years ago.
Improve this question
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
How to dynamic replace str to "1627207:132069:color:red;20518:28421:size:S".
It was a pretty unclear question, but I think I got it now. Your aliastr contains mappings which control the replacements, i.e., the key '20518:28421:' should map to value 'S' and the key '1627207:132069:' should map to 'red'. Then you want to search for those keys in str and replace their current value with that new value. This does that:
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
mapping = Hash[aliastr.scan(/(\d+:\d+:)(.*?)(?:;|$)/)]
# mapping = {"20518:28421:"=>"S", "20518:28358:"=>"L", "20518:28357:"=>"M", "1627207:132069:"=>"red"}
replaced = str.gsub(/(\d+:\d+:)(\w+:).*?(;|$)/) do |match|
key = $1
value = mapping[$1]
key + $2 + value + $3
end
p replaced
# => "1627207:132069:color:red;20518:28421:size:S"
Your question is not very clear, and probably contains an error ("color:red" in your wanted result vs. "red" in aliastr).
You may try something like this:
str = "1627207:132069:color:green;20518:28421:size:62cm"
aliastr = "20518:28421:S;20518:28358:L;20518:28357:M;1627207:132069:red"
replacements = aliastr.split(";").map{|s| parts=s.split(":"); [/#{parts[0]}:#{parts[1]}:.*/,s]}
src = str.split(";")
src.map{|s| replacements.each{|r| s.sub!(r[0],r[1])}; s }.join(";")