[英]Regex capture word follow by a number as a group
I need to match this string:我需要匹配这个字符串:
Cattle: Sheep: Pigs:
Meat –0 days Meat – 0 days Meat – 0 days
Milk – 0 hours Milk – 0 hours
To create the following capture groups:要创建以下捕获组:
[cattle:, sheep:, pigs:]
[meat - 0 days, meat - 0 days, meat - 0 days]
[milk - 0 days, milk - 0 days]
I've tried to have a go myself but I am getting nowhere, I currently have something like this but it matches digits and spaces all over the string我试过自己试一试,但我一无所获,我目前有这样的东西,但它匹配整个字符串的数字和空格
(?!Cattle|Sheep|Pigs)(\b[Meat\s-\s\d*]\b)
I am capturing the cattle, speed and pigs strings in a different regex, hence the neg lookahead.我在不同的正则表达式中捕获了牛、速度和猪的字符串,因此是否定的前瞻。
EDIT: Sorry I should have specific more.编辑:对不起,我应该有更多的具体信息。 These are all on a separate line split into an array.这些都在一个单独的行上,分成一个数组。
str =<<END
Cattle: Sheep: Pigs:
Meat –0 days Meat – 0 days Meat – 0 days
Milk – 0 hours Milk – 0 hours
END
str.each_line.map { |line| line.chomp.downcase.split(/(?<=:|days|hours) */) }
#=> [["cattle:", "sheep:", "pigs:"],
# ["meat –0 days", "meat – 0 days", "meat – 0 days"],
# ["milk – 0 hours", "milk – 0 hours"]]
The regular expression reads, "match one or more spaces that are immediately preceded by a colon, the word 'days' or the word 'hours', (?<=:|days|hours)
being a positive lookbehind .正则表达式读作:“匹配一个或多个紧跟在冒号前面的空格,单词 'days' 或单词 'hours', (?<=:|days|hours)
是一个积极的lookbehind 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.