Click to See Complete Forum and Search --> : RegularExpressionValidator
Toot
January 6th, 2008, 06:27 PM
Hi there.
I want to allow the user to select any but a couple of items in a listbox. I've got a RegularExpressionValidator for the listbox but I'm somewhat stuck on the regular expression - how do I say NOT string a or b?
"^(a|b)$" is exactly wrong but I don't seem to be able to say NOT "^(a|b)$" ... any clues???
Thanks for your time,
T
wildfrog
January 7th, 2008, 04:56 AM
If you need to match any one-character string , except "A" AND "B", try
^[^AB]$
If you need to match any string not containing the letters 'A' or 'B', try
[^AB]
Or if you need to match any string not beginning with the letter 'A' or 'B':
^[^AB]
- petter
Toot
January 7th, 2008, 05:26 AM
Thanks for the reply. I'm trying to match a string, rather than character set - for example NOT ("Apples" OR "Pears"):
NOT (and that's the bit I'm having trouble with) "^(Apples|Pears)$".
Thanks again,
T
wildfrog
January 7th, 2008, 05:46 AM
Ok, then you can use negative lookahead.
To match a string, except "Apples" or "Pears":
^(?!Apples$|Pears$).*$
Or, to match any string except thoes begginning with "Apples" or "Pears", try:
^(?!Apples|Pears).*$
- petter
Toot
January 7th, 2008, 05:29 PM
That's exactly it - you're brilliant and amazing. I wasn't even on the right track! Regular expressions are indeed a world unto their own...
Thanks again,
T.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.