Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

how to use NOT operator

Last post 12-01-2008, 11:15 AM by mash. 2 replies.
Sort Posts: Previous Next
  •  12-01-2008, 3:19 AM 49008

    how to use NOT operator

    I am trying to use NOT operator with no success.

     My target is to test a string to make sure it is not "SuperAdmin" and i am unable to find any way of it

     [^SuperAdmin] doesn't work at all. Any help please.

     

     

  •  12-01-2008, 4:06 AM 49009 in reply to 49008

    Re: how to use NOT operator

    The stuff between two square brackets, [...], called a character class or character set, will always match a single character.
    For details, see: http://www.regular-expressions.info/charclass.html

    If you want to negate a string, try a negative-look-ahead:

    ^(?!SuperAdmin$).*$

    which will match any string except "SuperAdmin".

    A short explanation:

    ^                 // the start of the string
    (?!SuperAdmin$)   // NOT followed by '
    SuperAdmin' followed by the end of the string
    .*                // matches any character (except new line chars) zero or more times
    $                 // followed by the end of the string

  •  12-01-2008, 11:15 AM 49027 in reply to 49008

    Re: how to use NOT operator

    You mistake is a common one.  Here is another explanation of how the character class actually works. http://regexadvice.com/blogs/mash/archive/2008/01/31/A-touch-of-Character-Class.aspx

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
View as RSS news feed in XML