本文介绍了匹配字符串中连续字符的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串 "111221" 并且想要匹配所有连续相等的整数集:["111", "22", "1"].

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

我知道有一个特殊的正则表达式可以做到这一点,但我不记得了,而且我在谷歌上搜索很糟糕.

I know that there is a special regex thingy to do that but I can't remember and I'm terrible at Googling.

推荐答案

在 Ruby 1.8.7+ 中使用正则表达式:

Using regex in Ruby 1.8.7+:

p s.scan(/((\d)\2*)/).map(&:first)
#=> ["111", "22", "1"]

这是有效的,因为 (\d) 捕获任何数字,然后 \2* 捕获该组(第二个左括号)匹配的零个或多个.作为 scan 的结果,需要外部 (…) 来捕获整个匹配项.最后,scan 单独返回:

This works because (\d) captures any digit, and then \2* captures zero-or-more of whatever that group (the second opening parenthesis) matched. The outer (…) is needed to capture the entire match as a result in scan. Finally, scan alone returns:

[["111", "1"], ["22", "2"], ["1", "1"]]

...所以我们需要遍历并保留每个数组中的第一项.在 Ruby 1.8.6+ 中(为了方便,没有 Symbol#to_proc):

…so we need to run through and keep just the first item in each array. In Ruby 1.8.6+ (which doesn't have Symbol#to_proc for convenience):

p s.scan(/((\d)\2*)/).map{ |x| x.first }
#=> ["111", "22", "1"]

没有正则表达式,这是一个在 Ruby 1.9.2 中工作的有趣的(匹配任何字符):

With no Regex, here's a fun one (matching any char) that works in Ruby 1.9.2:

p s.chars.chunk{|c|c}.map{ |n,a| a.join }
#=> ["111", "22", "1"]

这是另一个即使在 Ruby 1.8.6 中也能工作的版本:

Here's another version that should work even in Ruby 1.8.6:

p s.scan(/./).inject([]){|a,c| (a.last && a.last[0]==c[0] ? a.last : a)<<c; a }
# => ["111", "22", "1"]

这篇关于匹配字符串中连续字符的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:23