找到至少有n个匹配元素的字符串

发布于 2024-11-15 02:28:22 字数 504 浏览 7 评论 0原文

我有一个数字列表,我想找到其中至少 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2024-11-22 02:28:22

您可以使用字符串替换将 - 替换为 |,将 01-02-03-10-11-12 转换为 01 |02|03|10|11|12。然后像这样包装它:

((01|02|03|10|11|12).*){3}

这将找到任何数字对,然后忽略任意数量的字符...... 3次。如果匹配,则成功。

You can use a string replacement to replace - with | to turn 01-02-03-10-11-12 into 01|02|03|10|11|12. Then wrap it like this:

((01|02|03|10|11|12).*){3}

This will find any of the digit pairs, then ignore any number of characters... 3 times. If it matches, then success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文