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

Expression to exclude two Strings

Last post 08-21-2008, 4:01 PM by jennifer. 6 replies.
Sort Posts: Previous Next
  •  08-21-2008, 10:18 AM 45538

    Expression to exclude two Strings

    This seems like it wouldn't be too difficult, but I am having lots of problems. I am using ASP.NET I have a form with a list of states using their two letter abbreviation. I have a regular expression validator and I would like for it to fail if the person chooses ME or WV from the list. I have have tried several expressions in Expresso and none seem to return the desired results.

    I tried [^ME]* but that excludes anything which contains M or E but I want it to only exclude items which contain both M and E.  

     

     

  •  08-21-2008, 3:05 PM 45541 in reply to 45538

    Re: Expression to exclude two Strings

    ^(?!ME|WV)[A-Z]{2}$
  •  08-21-2008, 3:18 PM 45542 in reply to 45541

    Re: Expression to exclude two Strings

    I tried the above reg ex ^(?!ME|WV)[A-Z]{2}$  in expresso. I had the sample text of

    ME
    AK
    MS
    DE
    WV

    However nothing is returned. I am not sure if I am doing somethig wrong or if there is an issue with the regex.

     

    I also tried the above regex at   using the above sample and no matches were returned.

  •  08-21-2008, 3:24 PM 45543 in reply to 45542

    Re: Expression to exclude two Strings

    ^(?!ME|WV)[A-Z]{2}$  would match these strings individually:

    "AK"

    "MS"

    "DE"

    But it won't match the input you attempted since your text looks like this to a regex operation:

    ^ME\r\nAK\r\nMS\r\nDE\r\nWV$

    Note that my pattern was meant to test one state abbreviation at a time.


  •  08-21-2008, 3:28 PM 45544 in reply to 45543

    Re: Expression to exclude two Strings

    I am sorry maybe I am not understanding something. I tried again with

    ^(?!ME|WV)[A-Z]{2}$

    In the sample text I had just AK, however no matches were found.

  •  08-21-2008, 3:37 PM 45545 in reply to 45544

    Re: Expression to exclude two Strings

    Possibly the copy-and-paste of the pattern left a space at the end.

    See the pattern and sample text here:

    http://www.myregextester.com/?r=289


  •  08-21-2008, 4:01 PM 45547 in reply to 45545

    Re: Expression to exclude two Strings

    You are exactly right, the reg ex is correct, I had copied over an extra space. Thanks for all your help!
View as RSS news feed in XML