ruby - Elimination of extra characters in a word -


i'm doing project requires removal of letters in word.

if letter occurs 3 or more times consecutively, condense 1 letter

-happyyyyyy -> happy -awwwsum -> awsum -cooool -> col 

i'm using ruby 1.8.7 this. how go this?

here's how it:

result = subject.gsub(     /(.)   # match single character, capture in group 1     \1{2,} # match same character 2 or more times, many possible/x,     '\1')   # replace 1 captured character 

result:

> subject = "happyyyy daaaaays!!!" => "happyyyy daaaaays!!!" > result = subject.gsub(/(.)\1{2,}/, '\1') => "happy days!" 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -