This question already has answers here:
Using Nokogiri to Split Content on BR tags
(2 answers)
Closed 5 years ago.
I am using Nokogiri to search for some information on a webpage. After I find it I get the following string:
<p></p>
<p>Spiegel Institut Communication GmbH & Co. KG i. G.<br>Eastsite
VI<br>Hermsheimer Straße 5<br>D-68163 Mannheim<br>Telefon: +49 621-728 44-
444<br>Telefax: +49 621-728 44-445<br>E-Mail: b.weber#spiegel-
institut.de<br>Geschäftsführer:<br>Dipl.-Kfm. Götz Spiegel<br>Amtsgericht
Mannheim<br>Inhaltlich Verantwortlicher gemäß § 10 Absatz 3 MDStV: Götz
Spiegel (Anschrift wie oben)</p>
It is all a single line. I would like to separate the String on the into a new line so I can get the single details from it. How do I go about it? I am new to Ruby and I have tried but somehow I cannot get the results I want.
Thank you very much
It's relatively simple, using the split method on the string. For example:
'this<br>that<br> the other'.split("<br>")
Will result in
["this", "that", " the other"]
you can just use the split method:
string.split('<br>')
Related
This question already has answers here:
How do I find if a string starts with another string in Ruby?
(4 answers)
Closed 3 years ago.
image_basename = 'fr-ca-test.png'
Langs = {'ca', 'fr-CA', 'en-CA'}
Langs.each do |locale_code|
return locale_code /(\b|\_|-)#{locale_code}(\b|\_|-)/i.match(image_basename)
end
end
When the filename contains fr-CA or en-CA. I would like to returns fr-CA not Ca.
How I can fix my regex?
I wouldn't use a regexp in this simple example. Using start_with? will very likely be faster and IMHO it is easier to read and to understand:
image_basename = 'fr-ca-test.png'
LANGUAGES = ['fr-CA', 'en-CA', 'ca']
LANGUAGES.find { |code| image_basename.start_with?(code.downcase) }
#=> "fr-CA"
This question already has answers here:
Ruby: Extracting Words From String
(5 answers)
Closed 6 years ago.
I want get words from string.
For example:
str = "Mike's book.".
I wish I can get ["Mike's", "book"].
I know we can str.split(/\W+/), but it will return ["Mike", "s", "book"], that's not what I want.
Use scan() method in Ruby with character class regex including word character and single quote.
str.scan(/[\w']+/)
This question already has answers here:
Ruby combining an array into one string
(4 answers)
Closed 6 years ago.
i have an array like this
text_arr = ["hello","how","are","you"]
and i want to convert this to string like this
text = "hello how are you"
How can i do this with Ruby ?
Try this one
text = text_arr.join(' ')
This question already has answers here:
How can I detect common substrings in a list of strings
(9 answers)
Closed 7 years ago.
How to automatically extract the common characters or common string from a set of input strings? is there an algorithm that does this?
I am trying to figure out how to parse 1000 input strings and automatically create groups of string based on the largest matching patterns.
Is there a library in ruby which does this?
Sample Input
What is your name?
Who wrote this book?
Your name starts with ABC
Is this book good?
Why is your name so long?
Have you read this book?
Expected Output.
your name
——————
What is your name?
Your name starts with ABC
Why is your name so long?
this book
————
Who wrote this book?
Have you read this book?
Is this book good?
Edited to clarify and fixed an error based on luqui's comment.
The case doesn't matter.
You can use core Ruby library:
["your name", "book"].map do |substring|
[substring, text.lines.map(&:downcase).select { |line| line[substring] }]
end.to_h
# => {
# "your name" => ["What is your name?", "Your name starts with ABC", ...],
# "book" => [...]
# }
This question already has answers here:
How to format a number 1000 as "1 000"
(12 answers)
Closed 8 years ago.
I was working on a method to add commas to a number that is passed. I.E. separate_commas(1000) would return "1,000" or separate_commas(100000) would return "100,000"...etc.
Now that I've solved it, I'm wondering how I could refactor without regular expressions. Would appreciate suggestions, thank you in advance. Ruby 2.1.1p76
def separate_comma(x)
x=x.to_s
len=-4
until len.abs > x.length
x.insert(len, ',')
len-=4
end
return x
end
Not exactly pretty, but it is a little more ruby-esque
num.to_s.reverse
.split('').each_slice(3).to_a
.map{|num| num.reverse.join('')}.reverse.join(',')