[英]Ruby split binary string where the previous character is different from the next one
I wonder how could I split a binary string in Ruby. 我想知道如何在Ruby中拆分二进制字符串。 I want to split the string where the previous character is different from the next one.
我想在前一个字符与下一个字符不同的地方分割字符串。
for example if i have the string 例如,如果我有字符串
@s = "aaaabbabbaa"
I would like to create an array of strings 我想创建一个字符串数组
@array[0] = "aaaa"
@array[1] = "bb"
@array[2] = "a"
@array[3] = "bb"
@array[4] = "aa"
How could i do this? 我该怎么办?
Enumerable#chunk does that, but its defined on Enumerable - and String does not include Enumerable. Enumerable#chunk可以做到这一点,但是它在Enumerable上定义-并且String不包含Enumerable。 Transform it into an Array of chars (and glue them back to strings) , like:
将其转换为一个字符数组(并将其粘贴回字符串),例如:
s = "aaaabbabbaa"
p array = s.chars.chunk(&:itself).map{|a| a.last.join} #=>["aaaa", "bb", "a", "bb", "aa"]
You could use a regular expression with scan
: 您可以对
scan
使用正则表达式:
@array = @s.scan(/((.)\2*)/).map(&:first)
#=> ["aaaa", "bb", "a", "bb", "aa"]
str = "aaaabbabbaa"
r = /
(?<=(.)) # match any character in capture group 1, in positive lookbehind
(?!\1) # do not match capture group 1, negative lookahead
/x # free-spacing regex definition mode
str.split(r)
#=> ["aaaa", "a", "bb", "b", "a", "a", "bb", "b", "aa", "a"]
By using two lookarounds no characters are lost when splitting on the regular expression. 通过使用两个环顾四周,在正则表达式上分割时不会丢失任何字符。
using Enumerable#chunk_while 使用Enumerable#chunk_while
str = "aaaabbabbaa"
p str.chars.chunk_while(&:==).map(&:join)
Output : ["aaaa", "bb", "a", "bb", "aa"]
输出:
["aaaa", "bb", "a", "bb", "aa"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.