简体   繁体   English

正则表达式从Ruby中的单个字符串中提取几种模式的所有匹配项

[英]Regex to extract all matches of several patterns from a single string in Ruby

After three hours I'm going to stop banging my head on the wall and ask for help. 三个小时后,我将停止在墙上敲打头,寻求帮助。 This ought to be an easy regex, but I just can't quite get my mind around it today. 这应该是一个简单的正则表达式,但是今天我只是不太了解。

Given a short string and a few different patterns, I need to extract all the matching patterns from the string, where each pattern is a "whole word". 给定一个短字符串和一些不同的模式,我需要从字符串中提取所有匹配的模式,其中每个模式都是一个“整个单词”。

The patterns are: "YES", "NO", "MAYBE" , any sequence of asterisks ['*', '**', '***', '...'] , any number from 0 to 999, with the case of the text being case-insensitive. 模式是: "YES", "NO", "MAYBE" ,星号的任何序列['*', '**', '***', '...'] ,从0到999的任意数字,文字不区分大小写。

So if s = 'foo yes and no doo 5 boo **** bar far 13 not but no' the result would be ["yes", "no", "5", "****", "13", "no"] 因此,如果s = 'foo yes and no doo 5 boo **** bar far 13 not but no' ,结果将为["yes", "no", "5", "****", "13", "no"]

s = 'foo yes and no doo 5 boo * *** bar far 13 not but no YESTERDAY'
ms = s.scan(/(?:\b(?:yes|no|\d{1,3})\b|\*+)/i)
ms # => ["yes", "no", "5", "*", "***", "13", "no"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM