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.
var nospecial=/^[^* | \ " : < > [ ] { } ` \ ( ) '' ; @ & $]+$/; if(address. match(nospecial)){ alert('Special characters like * | \ " : < > [ ] { } ` \ ( ) \'\' ; @ & $ are not allowed'); return false; but it is not working.
$ 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With