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
I can do this easily using 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 AAA
would 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.
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 moreA
,B
,C
, or|
character, followed by any number of characters, followed by one or moreA
,B
,C
, or|
character, followed by any number of characters.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:
This will match any string that contains
A
,B
, andC
in any order.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
Start with a basic word match:
to require the word matched with
\w+
begins withun
, we could use a positive lookaheadWhat this says is
\b
Match a blank(?=un)
Are there the letters "un"? If not, NO MATCH. If so, then possible match.\w+
One or more word characters\b
Match a blankA 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 WITHun
. If it does not, then the\w+
expression won't match.How about matching any words that do not begin with
un
? Simply use a "negative lookahead"\b
Match a blank(?!un)
Are there the letters "un"? If SO, NO MATCH. If not, then possible match.\w+
One or more word characters\b
Match a blankSo for your requirement of having at least 1 A, 1 B and 1 C in the string, a pattern like
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)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.
If str is your string, maybe something like:
str.contains('A') && str.contains('B') && str.contains('C')
You can make use of positive lookaheads:
(?=.*A)
makes sure there's an A somewhere in the string and the same logic applies to the other lookaheads.