I'm attempting to produce a human readable wiki-like difference between 2 bodies of html laden text. I'm using diff-lcs and the first step is separating the string (array of characters) into an array of sentences, but keep their punctuation.
"I am a lion. Hear me roar! Where is my cub? Never mind, found him.".magic_split(/[.?!]/)
# => "I am a lion." "Hear me roar!" "Where is my cub?" "Never mind, found him."
This should do the trick
"I am a lion. Hear me roar! Where is my cub? Never mind, found him.".gsub(/[.?!]/, '\1|').split('|')
Except gsub appears to have trouble inserting the characters .?!
. Instead it's returning this
"I am a lion| Hear me roar| Where is my cub| Never mind, found him|"
What's the easiest way to do a non-destructive split? As in it keeps the characters it splits by.
scan
should do the trick (throw strip
in there to get rid of trailing spaces).
s = "I am a lion. Hear me roar! Where is my cub? Never mind, found him."
s.scan(/[^\.!?]+[\.!?]/).map(&:strip) # => ["I am a lion.", "Hear me roar!", "Where is my cub?", "Never mind, found him."]
I think that should be \0
>> string = "I am a lion. Hear me roar! Where is my cub? Never mind, found him."
>> string.gsub(/[.?!]/, '\0|')
# "I am a lion.| Hear me roar!| Where is my cub?| Never mind, found him.|"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With