Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping non-alphanumeric chars but leaving spaces in Ruby

Tags:

string

ruby

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

like image 471
RailsTweeter Avatar asked Apr 09 '12 12:04

RailsTweeter


3 Answers

You can simply add \s (whitespace)

string.downcase.gsub(/[^a-z0-9\s]/i, '')

like image 191
gmalette Avatar answered Jan 10 '23 18:01

gmalette


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 ひらがな"
like image 20
fl00r Avatar answered Jan 10 '23 18:01

fl00r


Some fine solutions, but simplest is usually best:

string.downcase.gsub /\W+/, ' '
like image 31
pguardiario Avatar answered Jan 10 '23 18:01

pguardiario