Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Regex Gsub when does not match

Tags:

regex

ruby

gsub

I want to gsub all characters in a string that are not letters and replace with '#'. I think I need a regular expression that goes something like, "gsub() when this regex does not match."

Any ideas?

like image 410
Mike Avatar asked Jan 17 '11 16:01

Mike


2 Answers

Look ma, no regex...

str.tr( '^A-Za-z', '#' )
like image 167
steenslag Avatar answered Sep 30 '22 21:09

steenslag


str.gsub(/[^a-zA-Z]/, '#')

The ^ means doesn't match

like image 27
Stefaan Colman Avatar answered Sep 30 '22 21:09

Stefaan Colman