简体   繁体   English

如何在Ruby中匹配多个正则表达式模式

[英]How to match multiple regex patterns in ruby

I see couple of questions on multiple regex patterns in different contexts but I am unable to get a grip on it. 我在不同的上下文中看到了多个有关正则表达式模式的问题,但我无法掌握。

I have a string str = "Hello, how are you. Hello, I am lloyds" in which I would like to apply multiple patterns to extract all Hello s and all ll s in one go to get ["Hello", "Hello", "ll", "ll", "ll"] . 我有一个字符串str = "Hello, how are you. Hello, I am lloyds"在这里我想应用多种模式来一次提取所有Helloll以获得["Hello", "Hello", "ll", "ll", "ll"] How do I do it? 我该怎么做?

The only way I was able to do is (which is not multiple patterns in one go) 我唯一能做的就是(一口气不是多个模式)

str = "Hello, how are you. Hello, I am lloyds"
a = []
a << str.scan(/Hello/)
a << str.scan(/ll/)
a.flatten

Because "ll" is inside "Hello", logic to include both in same scan method call requires a slightly clumsy-looking expression that double-captures the "ll". 因为“ ll”在“ Hello”内部,所以要在相同的扫描方法调用中同时包含这两者,就需要一个看上去有点笨拙的表达式,该表达式要双重捕获“ ll”。 This seems close, but note the sequence interleaves "Hello" and "ll", unlike the expected output. 这似乎很接近,但是请注意,序列与预期的输出不同,是交错的“ Hello”和“ ll”。 However, as far as I can see, that would be a necessity for any regular expression that makes a single pass through the string: 但是,据我所知,这对于任何一次通过字符串的正则表达式都是必要的:

str = "Hello, how are you. Hello, I am lloyds"
a = str.scan( /(He(ll)o|ll)/ ).flatten.compact
 => ["Hello", "ll", "Hello", "ll", "ll"]

The compact is necessary, because a lone "ll" will not match the inner capture, and the array may contain unwanted nil s. 紧凑是必要的,因为一个单独的“ ll”将不匹配内部捕获,并且数组可能包含不需要的nil

str = "Hello, how the hello are you. Hello, I am lloyds"
results = []

str.scan(/hello|ll/xmi) do |match|
  target = match.downcase
  results.unshift match if target == 'hello'
  results << 'll'
end

p results

--output:--
["Hello", "hello", "Hello", "ll", "ll", "ll", "ll"]

Or: 要么:

str = "Hello, how the hello are you. Hello, I am lloyds"
hello_count = 0
ll_count = 0

str.scan(/Hello|ll/xm) do |match|
  hello_count += 1 if match == 'Hello'
  ll_count += 1 
end

results = ["Hello"] * hello_count + ["ll"] * ll_count 
p results

--output:--
["Hello", "Hello", "ll", "ll", "ll", "ll"]

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

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