找到至少有n个匹配元素的字符串
我有一个数字列表,我想找到其中至少 3 个... 这是一个例子,
我在 sql 数据库中有一个很大的数字列表,格式为(例如)
01-02-03-04-05-06
06-08-19-24-25-36
等 基本上是 0 到 99 之间的 6 个随机数。
现在我想找到一组给定数字中至少出现 3 个的字符串。 例如:
给出:01-02-03-10-11-12 返回其中至少包含 3 个这些数字的字符串。 例如,
01-05-06-09-10-12 would match
03-08-10-12-18-22 would match
03-09-12-18-22-38 would not
我在想可能有一些算法甚至正则表达式可以匹配这个......但我认为我缺乏计算机科学教科书经验让我绊倒。
不——这不是家庭作业问题!这是针对实际应用的!
我正在用红宝石开发,但任何语言答案将不胜感激
I have a list of numbers that I want to find at least 3 of...
here is an example
I have a large list of numbers in a sql database in the format of (for example)
01-02-03-04-05-06
06-08-19-24-25-36
etc etc
basically 6 random numbers between 0 and 99.
Now I want to find the strings where at least 3 of a set of given numbers occurs.
For example:
given: 01-02-03-10-11-12
return the strings that have at least 3 of those numbers in them.
eg
01-05-06-09-10-12 would match
03-08-10-12-18-22 would match
03-09-12-18-22-38 would not
I am thinking that there might be some algorithm or even regular expression that could match this... but my lack of computer science textbook experience is tripping me up I think.
No - this is not a homework question! This is for an actual application!
I am developing in ruby, but any language answer would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用字符串替换将
-
替换为|
,将01-02-03-10-11-12
转换为01 |02|03|10|11|12
。然后像这样包装它:这将找到任何数字对,然后忽略任意数量的字符...... 3次。如果匹配,则成功。
You can use a string replacement to replace
-
with|
to turn01-02-03-10-11-12
into01|02|03|10|11|12
. Then wrap it like this:This will find any of the digit pairs, then ignore any number of characters... 3 times. If it matches, then success.