仅当模式的长度大于某个值时,正则表达式才匹配该模式

发布于 2024-11-24 07:23:59 字数 697 浏览 1 评论 0原文

我有 2 个字符串,想知道

  1. 字符串 2 是否具有字符串 1 的字符子集 相同的顺序

  2. 此子集具有由我确定的最小大小。

例如:

string 1 = stackoverflow
<br>minimun size = 5

string 2a = stack (MATCH)
string 2b = stac  (DO NOT MATCH)
string 2c = staov (MATCH)
string 2d = staZov (DO NOT MATCH)
string 2e = eflow (MATCH) 
string 2f = ewolf (DO NOT MATCH)
string 2g = somethingstacksomething (MATCH) 

我正在以编程方式构建正则表达式,因此问题的第一部分可以使用表达式来解决:(s)?(t)?(a)?(c)?(k)?(o) ?(v)?(e)?(r)?(f)?(l)?(o)?(w)?

但我不知道如何应用“最小字符”规则。是否可以使用正则表达式来做到这一点?

提前致谢!

编辑:添加了另一个示例来完成问题规范。另外,如果您想知道,这是评估用户指定的密码强度的方法的一部分。如果他定义了从其他信息(登录名、出生日期、姓名等)派生的密码,我们应该警告他。

I have 2 strings and want to know

  1. if string 2 have a subset of the characters of string 1 on the
    same order

  2. this subset has a minimun size determined by me.

For example:

string 1 = stackoverflow
<br>minimun size = 5

string 2a = stack (MATCH)
string 2b = stac  (DO NOT MATCH)
string 2c = staov (MATCH)
string 2d = staZov (DO NOT MATCH)
string 2e = eflow (MATCH) 
string 2f = ewolf (DO NOT MATCH)
string 2g = somethingstacksomething (MATCH) 

I am building the regex programatically, so the first part of the problem could be solved with the expression: (s)?(t)?(a)?(c)?(k)?(o)?(v)?(e)?(r)?(f)?(l)?(o)?(w)?

But i can't figure out how to apply the "minimun character" rule. Is it possible to do it using regex?

thanks in advance!

EDIT: Added another example to complete the problem specification. Also, if you want to know, this is part of a method to evaluate the password strength specified by a user. If he defines a password derived from some other information (login, born date, name, etc) we should warn him.

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

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

发布评论

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

评论(3

我ぃ本無心為│何有愛 2024-12-01 07:23:59

您可以添加一个前瞻,确保有五个字符:(?=.{5})s?t?a?c?k?o?v?e?r?f?l?o?w?

You could add a lookahead that ensures that there are five characters: (?=.{5})s?t?a?c?k?o?v?e?r?f?l?o?w?

韶华倾负 2024-12-01 07:23:59

但我不知道如何应用“最小字符”规则。

确定可能的匹配后。您需要确定结果的长度。

您可以简单地使用String.Length来确定。

But i can't figure out how to apply the "minimun character" rule.

After you determine the possible matches. You need to determine the length of the result.

You can simply use String.Length to determine that.

何止钟意 2024-12-01 07:23:59

有趣的问题!有一种方法可以做到这一点(但你不会喜欢它)。它会导致可能很大(并且可能很慢)的正则表达式。假设string1ABCD。然后,这里是满足所有不同长度要求的正则表达式:

length  = 4;
regex = @"
  ABCD
  ";

length  = 3;
regex = @"
    ABCD?
  | ABC?D
  | AB?CD
  | A?BCD
  ";

length  = 2;
regex = @"
    ABC?D?
  | AB?CD?
  | AB?C?D
  | A?BCD?
  | A?BC?D
  | A?B?CD
  ";

length  = 1
regex = @"
  [ABCD]
  ";

我还没有想好以编程方式组装它的方式,但我确信它可以完成。但是,具有大量“或”替代项的大型正则表达式可能会比使用您当前使用的“匹配然后检查匹配字符串的长度”方法慢。

我在这个问题上摸索了很长一段时间,我很确定没有简单/聪明的正则表达式构造可以满足您的要求。

Interesting question! There is a way to do it, (but you aren't going to like it). It results in a potentially large (and possibly slow) regex. Assuming string1 is ABCD. Then here are the regexes for all the different length requirements:

length  = 4;
regex = @"
  ABCD
  ";

length  = 3;
regex = @"
    ABCD?
  | ABC?D
  | AB?CD
  | A?BCD
  ";

length  = 2;
regex = @"
    ABC?D?
  | AB?CD?
  | AB?C?D
  | A?BCD?
  | A?BC?D
  | A?B?CD
  ";

length  = 1
regex = @"
  [ABCD]
  ";

I haven't thought through the way you would go about assembling this programmatically, but I'm sure that it can be done. However, a large regex having lots of OR'ed alternatives may likely be slower that using the "match then check the length of the matched string" method you are currently using.

I scratched my head for quite a while on this one and I'm pretty sure there is no simple/clever regex construct that does what you are asking.

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