匹配字符串中连续字符的序列

编程入门 行业动态 更新时间:2024-10-14 02:20:54
本文介绍了匹配字符串中连续字符的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有字符串 "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"]

更多推荐

匹配字符串中连续字符的序列

本文发布于:2023-10-28 04:37:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!