Ruby: Understanding Regex
I am trying to write a script that will match all words in a string and will strip all non words (IE: . [dot], ampersands, colons, etc) out and will replace them with a hyph开发者_C百科en.
Example String:
L. L. Cool J & Sons: The Cool Kats
Example Output:
L-L-Cool-J-Sons-The-Cool-Kats
Here is some code I am working with:
str = "L. L. Cool J & Sons: The Cool Kats"
str.scan(/\w+/)
Thanks for all the help! I am still pretty new to regex
In a single line, find all bits of text that aren't "word characters" and replace with a dash:
str.gsub(/\W+/, '-')
Note that "word characters" includes numbers and underscores. To just allow letters you could use the following:
str.gsub(/[^A-Za-z]+/, '-')
Update: I just noticed that the two calls can be expressed as one:
str.gsub(/\W+/, '-')
=> "L-L-Cool-J-Sons-The-Cool-Kats"
...which results to the same as Narendra's answer or my original answer:
# 1st gsub: replace all non-words with hyphens
# 2nd gsub: replace multiple hyphens with a single one
str.gsub(/\W/,'-').gsub(/-+/, '-')
=> "L-L-Cool-J-Sons-The-Cool-Kats"
str.gsub(/\W/, '-') #=> replaces all non-words with space
# OR
str.gsub(/[^A-Za-z\s]/, ' ') #=> replace all non letters/spaces with space
str.gsub(/\s+/, '-') #=> replaces all groups of spaces to hypens
Input:
L. L. Cool J & Sons: The Cool Kats
Output:
L-L-Cool-J-Sons-The-Cool-Kats
You can use \W
for non word characters. It should do your job. \w
stands for word characters. I don't work much with ruby but it should look something like this
result = subject.gsub(/\W+/, '-')
.
精彩评论