仅当模式的长度大于某个值时,正则表达式才匹配该模式
我有 2 个字符串,想知道
字符串 2 是否具有字符串 1 的字符子集 相同的顺序
此子集具有由我确定的最小大小。
例如:
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
if string 2 have a subset of the characters of string 1 on the
same orderthis 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加一个前瞻,确保有五个字符:
(?=.{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?
确定可能的匹配后。您需要确定结果的长度。
您可以简单地使用String.Length来确定。
After you determine the possible matches. You need to determine the length of the result.
You can simply use String.Length to determine that.
有趣的问题!有一种方法可以做到这一点(但你不会喜欢它)。它会导致可能很大(并且可能很慢)的正则表达式。假设
string1
是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
isABCD
. Then here are the regexes for all the different length requirements: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.