Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex must contain specific letters in any order

Tags:

regex

vb.net

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.

like image 250
Sam Johnson Avatar asked Sep 05 '13 18:09

Sam Johnson


People also ask

How do you restrict special characters in regex?

var nospecial=/^[^* | \ " : < > [ ] { } ` \ ( ) '' ; @ & $]+$/; if(address. match(nospecial)){ alert('Special characters like * | \ " : < > [ ] { } ` \ ( ) \'\' ; @ & $ are not allowed'); return false; but it is not working.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I match a specific character in regex?

There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.

Does order matter in regex?

The order of the characters inside a character class does not matter. The results are identical. You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9.


1 Answers

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.

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 C in any order.

like image 172
p.s.w.g Avatar answered Oct 05 '22 00:10

p.s.w.g