[英]Ruby Regex match multiple times
I want to find a search term in a given word. 我想在给定单词中找到搜索词。 So we're speaking Regex.
所以我们在说正则表达式。
The order of the letters is important, but I want to allow letters between each of the search term's letters. 字母的顺序很重要,但是我希望每个搜索词的字母之间都可以有字母。
An example : the german word seitenschneider contains the word seide in at least two ways: 例如 :德语单词seitenschneider至少以两种方式包含seide单词:
word: seitenschneider
match 1: xxx.........xx.
match 2: ......x...xxxx.
I want to find the result with the least possible letters, so in this case I'd go with match 2. 我希望找到尽可能少的字母的结果,因此在这种情况下,我将使用第2个匹配项。
Is there a way to do this with regex? 有没有办法用正则表达式做到这一点?
I tried making the wildcards ungreedy but didn't receive the desired result: 我尝试使通配符不愉快,但没有收到预期的结果:
"seitenschneider".scan(/s.*?e.*?i.*?d.*?e/)
=> ["seitenschneide"]
What I want to achieve is: 我想要实现的是:
"seitenschneider".scan(magic_regex_thingy)
=> ["schneide"]
I'd also be happy with something like 我也会喜欢这样的东西
"seitenschneider".scan(another_magic_regex_thingy)
=> ["seitenschneide", "schneide"]
cause I can find the shortest word in there by myself. 因为我可以自己找到最短的单词。
Any hints on how to get there? 关于如何到达那里的任何提示?
For your second question: 对于第二个问题:
"seitenschneider".scan(/(?=(s.*?e.*?i.*?d.*?e))/).flatten
# => ["seitenschneide", "schneide"]
Getting the shortest one using the regex above: 使用上面的正则表达式获取最短的一个:
"seitenschneider".scan(/(?=(s.*?e.*?i.*?d.*?e))/).flatten.min_by(&:length)
# => "schneide"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.