CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    RegularExpressionValidator

    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
    Some cause happiness wherever they go; others, whenever they go.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: RegularExpressionValidator

    If you need to match any one-character string , except "A" AND "B", try
    Code:
    ^[^AB]$
    If you need to match any string not containing the letters 'A' or 'B', try
    Code:
    [^AB]
    Or if you need to match any string not beginning with the letter 'A' or 'B':
    Code:
    ^[^AB]
    - petter

  3. #3
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: RegularExpressionValidator

    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
    Some cause happiness wherever they go; others, whenever they go.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: RegularExpressionValidator

    Ok, then you can use negative lookahead.

    To match a string, except "Apples" or "Pears":
    Code:
    ^(?!Apples$|Pears$).*$
    Or, to match any string except thoes begginning with "Apples" or "Pears", try:
    Code:
    ^(?!Apples|Pears).*$
    - petter

  5. #5
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: RegularExpressionValidator

    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.
    Some cause happiness wherever they go; others, whenever they go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured