Trying to change this:
"The basketball-player is great! (Kobe Bryant)"
into this:
"the basketball player is great kobe bryant"
Want to downcase and remove all punctuation but leave spaces...
Tried string.downcase.gsub(/[^a-z ]/, '')
but it removes the spaces
You can simply add \s
(whitespace)
string.downcase.gsub(/[^a-z0-9\s]/i, '')
If you want to catch non-latin characters, too:
str = "The basketball-player is great! (Kobe Bryant) (ひらがな)"
str.downcase.gsub(/[^[:word:]\s]/, '')
#=> "the basketballplayer is great kobe bryant ひらがな"
Some fine solutions, but simplest is usually best:
string.downcase.gsub /\W+/, ' '
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