正则表达式匹配 {1,2,3,4} 的所有排列而不重复

发布于 2024-09-06 21:07:27 字数 263 浏览 4 评论 0原文

我正在 ruby​​ 中实现以下问题。

这是我想要的模式:

1234、1324、1432、1423、2341 等等

,即四位数字中的数字应该在 [1-4] 之间,并且也应该不重复。

为了让您以简单的方式理解,我采用两位数模式 解决方案应该是: 12、21

,即数字应该是 1 或 2,并且不能重复。

为了确保它们不重复,我想使用 1 美元作为第二个数字的条件,但它不起作用。

请帮助我并提前致谢。

I am implementing the following problem in ruby.

Here's the pattern that I want :

1234, 1324, 1432, 1423, 2341 and so on

i.e. the digits in the four digit number should be between [1-4] and should also be non-repetitive.

to make you understand in a simple manner I take a two digit pattern
and the solution should be :
12, 21

i.e. the digits should be either 1 or 2 and should be non-repetitive.

To make sure that they are non-repetitive I want to use $1 for the condition for my second digit but its not working.

Please help me out and thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

热血少△年 2024-09-13 21:07:27

您可以使用它(参见 rubular.com):

^(?=[1-4]{4}$)(?!.*(.).*\1).*$

第一个断言确保它是 ^[1-4]{4}$,第二个断言是负向前瞻,确保您无法匹配 .*(.).*\1,即重复特点。第一个断言是“更便宜”,所以你要先这样做。

参考文献

相关问题

You can use this (see on rubular.com):

^(?=[1-4]{4}$)(?!.*(.).*\1).*$

The first assertion ensures that it's ^[1-4]{4}$, the second assertion is a negative lookahead that ensures that you can't match .*(.).*\1, i.e. a repeated character. The first assertion is "cheaper", so you want to do that first.

References

Related questions

我的痛♀有谁懂 2024-09-13 21:07:27

只是为了咯咯笑,这里有另一个选择:

^(?:1()|2()|3()|4()){4}\1\2\3\4$

当每个唯一字符被消耗时,它后面的捕获组捕获一个空字符串。反向引用还会尝试匹配空字符串,因此如果其中一个未成功,则只能意味着关联的组没有参与匹配。只有当字符串包含至少一个重复项时才会发生这种情况。

任何正则表达式风格均不正式支持这种空捕获组和反向引用的行为,因此请买者自负。但它适用于大多数,包括 Ruby。

Just for a giggle, here's another option:

^(?:1()|2()|3()|4()){4}\1\2\3\4$

As each unique character is consumed, the capturing group following it captures an empty string. The backreferences also try to match empty strings, so if one of them doesn't succeed, it can only mean the associated group didn't participate in the match. And that will only happen if string contains at least one duplicate.

This behavior of empty capturing groups and backreferences is not officially supported in any regex flavor, so caveat emptor. But it works in most of them, including Ruby.

岁吢 2024-09-13 21:07:27

我认为这个解决方案更简单一点

^(?:([1-4])(?!.*\1)){4}$

请参阅这里在 Rubular

^                  # matches the start of the string
    (?:            # open a non capturing group 
        ([1-4])    # The characters that are allowed the found char is captured in group 1
        (?!.*\1)   # That character is matched only if it does not occur once more
    ){4}           # Defines the amount of characters
$

(?!.* \1) 是一个 lookahead 断言,以确保字符不重复。

^$ 是匹配字符串开头和结尾的锚点。

I think this solution is a bit simpler

^(?:([1-4])(?!.*\1)){4}$

See it here on Rubular

^                  # matches the start of the string
    (?:            # open a non capturing group 
        ([1-4])    # The characters that are allowed the found char is captured in group 1
        (?!.*\1)   # That character is matched only if it does not occur once more
    ){4}           # Defines the amount of characters
$

(?!.*\1) is a lookahead assertion, to ensure the character is not repeated.

^ and $ are anchors to match the start and the end of the string.

全部不再 2024-09-13 21:07:27

虽然前面的答案解决了问题,但它们并不那么通用,并且不允许在初始字符串中重复。例如,{a,a,b,b,c,c}。在 类似问题后。 org/" rel="nofollow noreferrer">Perl Monks,以下解决方案Eily 给出:

^(?:(?!\1)a()|(?!\2)a()|(?!\3)b()|(?!\4)b()|(?!\5)c()|(?!\6)c()){6}$

同样,这适用于字符串中较长的“符号”,也适用于可变长度符号。

While the previous answers solve the problem, they aren't as generic as they could be, and don't allow for repetitions in the initial string. For example, {a,a,b,b,c,c}. After asking a similar question on Perl Monks, the following solution was given by Eily:

^(?:(?!\1)a()|(?!\2)a()|(?!\3)b()|(?!\4)b()|(?!\5)c()|(?!\6)c()){6}$

Similarly, this works for longer "symbols" in a string, and for variable length symbols too.

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