vb.net 正则表达式必须包含任何顺序的特定字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18643157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:57:31  来源:igfitidea点击:

Regex must contain specific letters in any order

regexvb.net

提问by Sam Johnson

I have been attempting to validate a string in VB.net that must contain these three letters in no particular order and do not need to be next to One another. ABC

我一直在尝试验证 VB.net 中的一个字符串,该字符串必须包含这三个字母,没有特定的顺序,并且不需要彼此相邻。美国广播公司

I can do this easily using LINQ

我可以使用 LINQ 轻松做到这一点

MessageBox.Show(("ABC").All(Function(n) ("AAAABBBBBCCCC").Contains(n)).ToString)

However, after searching Google and SO for over a week, I am completely stumped. My closest pattern is ".*[A|B|C]+.*[A|B|C]+.*[A|B|C]+.*"how ever AAAwould also return true. I know i can do this using other methods just after trying for a week i really want to know if its possible using One regular expression.

然而,在搜索谷歌和 SO 一个多星期后,我完全被难住了。我最接近的模式是".*[A|B|C]+.*[A|B|C]+.*[A|B|C]+.*"如何AAA也返回 true。我知道我可以在尝试一周后使用其他方法来做到这一点我真的想知道是否可以使用一个正则表达式。

采纳答案by p.s.w.g

Your original pattern won't work because it will match any number of characters, followed by one or more A, B, C, or |character, followed by any number of characters, followed by one or more A, B, C, or |character, followed by any number of characters, followed by one or more A, B, C, or |character, followed by any number of characters.

您的原始模式将不起作用,因为它将匹配任意数量的字符,后跟一个或多个A, B,C|字符,再后跟任意数量的字符,再后跟一个或多个A, B, C, 或|字符,后跟任意数量的字符,后跟一个或多个A, B, C, 或|字符,后跟任意数量的字符。

I'd probably go with the code you've already written, but if you really want to use a regular expression, you can use a series of lookahead assertions, like this:

我可能会使用您已经编写的代码,但如果您真的想使用正则表达式,则可以使用一系列先行断言,如下所示:

(?=.*A)(?=.*B)(?=.*C)

This will match any string that contains A, B, and Cin any order.

这将匹配任何包含字符串AB以及C以任何顺序。

回答by Jerry

You can make use of positive lookaheads:

您可以使用积极的前瞻:

^(?=.*A)(?=.*B)(?=.*C).+

(?=.*A)makes sure there's an A somewhere in the string and the same logic applies to the other lookaheads.

(?=.*A)确保字符串中某处有一个 A 并且相同的逻辑适用于其他前瞻。

回答by bobobobo

You can use zero-width lookaheads. Lookaheads are great to eliminate match possibilities if they don't meet a certain criteria.

您可以使用零宽度前瞻。如果不满足特定标准,前瞻非常适合消除匹配可能性。

For example, let's use the words

例如,让我们使用单词

untie queue unique block unity

Start with a basic word match:

从基本的单词匹配开始:

\b\w+\b

to require the word matched with \w+begins with un, we could use a positive lookahead

要要求匹配的单词以\w+开头un,我们可以使用正向前瞻

\b(?=un)\w+\b

What this says is

这说的是

  • \bMatch a blank
  • (?=un)Are there the letters "un"? If not, NO MATCH. If so, then possible match.
  • \w+One or more word characters
  • \bMatch a blank
  • \b匹配一个空白
  • (?=un)有字母“un”吗?如果不是,则不匹配。如果是,则可能匹配。
  • \w+一个或多个单词字符
  • \b匹配一个空白

A positive lookahead eliminates a match possibility if it does NOT meet the expression inside. It applies to the regex RIGHT AFTER it. So the (?=un)applies to the \w+expression above and requires that it BEGINS WITH un. If it does not, then the \w+expression won't match.

如果不满足内部表达式,则正向前瞻会消除匹配的可能性。它适用于它之后的正则表达式。所以 the(?=un)适用于\w+上面的表达式并要求它 BEGINS WITH un。如果不是,则\w+表达式将不匹配。

How about matching any words that do notbegin with un? Simply use a "negative lookahead"

匹配任何不以开头的单词怎么un样?只需使用“负前瞻”

\b(?!un)\w+\b
  • \bMatch a blank
  • (?!un)Are there the letters "un"? If SO, NO MATCH. If not, then possible match.
  • \w+One or more word characters
  • \bMatch a blank
  • \b匹配一个空白
  • (?!un)有字母“un”吗?如果是这样,则不匹配。如果不是,则可能匹配。
  • \w+一个或多个单词字符
  • \b匹配一个空白

So for your requirement of having at least 1 A, 1 B and 1 C in the string, a pattern like

因此,对于字符串中至少有 1 个 A、1 个 B 和 1 个 C 的要求,类似的模式

(?=.*A)(?=.*B)(?=.*C).+

Works because it says:

有效,因为它说:

  • (?=.*A)- Does it have .*any characters followed by A? If so, possible match if not no match.
  • (?=.*B)- Does it have .*any characters followed by B? If so, possible match if not no match.
  • (?=.*C)- Does it have .*any characters followed by C? If so, possible match if not no match.
  • .+If the above 3 lookahead requirements were met, match any characters. If not, then match no characters (and so there isn't a match)
  • (?=.*A)-.*后面有A 的字符吗?如果是,则可能匹配,否则可能匹配。
  • (?=.*B)-.*后面有 B 的字符吗?如果是,则可能匹配,否则可能匹配。
  • (?=.*C)-.*后面有 C 的字符吗?如果是,则可能匹配,否则可能匹配。
  • .+如果满足上述 3 个前瞻要求,则匹配任何字符。如果没有,则不匹配任何字符(因此不匹配)

回答by rix0r

Does it have to be a regex? That's something that can easily be solved without one.

它必须是正则表达式吗?这是一件无需任何人就可以轻松解决的事情。

I've never programmed in VB, but I'm sure there are helper functions that let you take a string, and query whether or not a character occurs in it.

我从来没有在 VB 中编程过,但我确信有一些辅助函数可以让你获取一个字符串,并查询其中是否出现一个字符。

If str is your string, maybe something like:

如果 str 是您的字符串,则可能类似于:

str.contains('A') && str.contains('B') && str.contains('C')

str.contains('A') && str.contains('B') && str.contains('C')