给定字符串:
strs = [
"foo",
" ",
"Hello \n there",
" Ooh, leading and trailing space! ",
]
我想要一个简单的方法,依次识别所有连续运行的空白和非空白字符,以及运行是否为空白:
strs.each{ |str| p find_whitespace_runs(str) }
#=> [ {k:1, s:"foo"} ],
#=> [ {k:0, s:" "} ],
#=> [ {k:1, s:"Hello"}, {k:0, s:" \n "}, {k:1, s:"World"} ],
#=> [
#=> {k:0, s:" "},
#=> {k:1, s:"Ooh,"},
#=> {k:0, s:" "},
#=> {k:1, s:"leading"},
#=> {k:0, s:" "},
#=> {k:1, s:"and"},
#=> {k:0, s:" "},
#=> {k:1, s:"trailing"},
#=> {k:0, s:" "},
#=> {k:1, s:"space!"},
#=> {k:0, s:" "},
#=> ]
{k:0, s:""}
这几乎可以工作,但只要字符串不以空格开头,就会包含一个前导组:
def find_whitespace_runs(str)
str.split(/(\S+)/).map.with_index do |s,i|
{k:i%2, s:s}
end
end
现实世界的动机:编写一个语法高亮器,在其他未分类的代码中区分空格和非空格。