Regex.IsMatch that simulates Case Statement

Posted on 2005-05-13
Last Modified: 2008-02-26
Is it possible to code a Regex.IsMatch that returns True if it finds exact matches of multiple values, simulating a Case Statement?  Pseudo code:

// Select Case MyString
// Case MyValue1,MyValue2,MyValue3
if (Regex.IsMatch(MyString,"MyValue1,MyValue2,MyValue3") {
    Console.WriteLine("MyString equals MyValue1 or MyValue2 or MyValue3");
// Case Else
else {
    Console.WriteLine("MyString doesn't equal MyValue1 or MyValue2 or MyValue3");
// End Select
Question by:arichexe
    LVL 96

    Expert Comment

    by:Bob Learned


    Author Comment

    I'm getting compile errors on the ^ and $ characters:

    if (!Regex.IsMatch(MyString,^("val1"|"val2"|"val3")$)
    LVL 96

    Accepted Solution

    if (!Regex.IsMatch(MyString,@"^(val1|val2|val3)$")


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now