Match sequences of consecutive characters in a string

前端 未结 4 1821
粉色の甜心
粉色の甜心 2020-12-19 05:31

I have the string \"111221\" and want to match all sets of consecutive equal integers: [\"111\", \"22\", \"1\"].

I know that there is a spe

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 06:19

    I found that this works, it first matches each character in one group, and then it matches any of the same character after it. This results in an array of two element arrays, with the first element of each array being the initial match, and then the second element being any additional repeated characters that match the first character. These arrays are joined back together to get an array of repeated characters:

    input = "WWBWWWWBBBWWWWWWWB3333!!!!"
    repeated_chars = input.scan(/(.)(\1*)/)
    # => [["W", "W"], ["B", ""], ["W", "WWW"], ["B", "BB"], ["W", "WWWWWW"], ["B", ""], ["3", "333"], ["!", "!!!"]]
    repeated_chars.map(&:join)
    # => ["WW", "B", "WWWW", "BBB", "WWWWWWW", "B", "3333", "!!!!"]
    

    As an alternative I found that I could create a new Regexp object to match one or more occurrences of each unique characters in the input string as follows:

    input = "WWBWWWWBBBWWWWWWWB3333!!!!"
    regexp = Regexp.new("#{input.chars.uniq.join("+|")}+")
    #=> regexp created for this example will look like: /W+|B+|3+|!+/
    

    and then use that Regex object as an argument for scan to split out all the repeated characters, as follows:

    input.scan(regexp)
    # => ["WW", "B", "WWWW", "BBB", "WWWWWWW", "B", "3333", "!!!!"]
    

提交回复
热议问题