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

Python Kivy ListView: How to delete selected ListItemButton? -

asp.net mvc 4 - A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name '' -