If I have a string such as \"aabbbbccffffdeffffgg\" and I wanted to split the string into this array: [\"aa\", \"bbbb\", \"cc\", \"ffffd\", \"e\", \"ffff\", \
\"aabbbbccffffdeffffgg\"
[\"aa\", \"bbbb\", \"cc\", \"ffffd\", \"e\", \"ffff\", \
You can use a regex with a back reference and the scan() method:
scan()
str = "aabbbbccffffdeffffgg" groups = [] str.scan(/((.)\2*)/) { |x| groups.push(x[0]) }
groups will look like this afterwards:
groups
["aa", "bbbb", "cc", "ffffd", "e", "ffff", "gg"]