Is there an easy way to test a Regular Expression for validity?

Posted on 2004-11-14
Last Modified: 2010-04-24
One of my controls lets the user type a Regular Expression that is used to evaluate the contents of another control.  Currently, I am testing to see if the expression is valid using exception handling like so:

    testRegEx = New RegEx(userText)
Catch ex As Exception
    'Invalid Regular Expression
 End Try

This works, but it seems wrong.  Exceptions are for failure-type scenarios, not common error checking.  Is there a way to evaluate a string and determine if it is a valid Regular Expression without creating my own RegEx parser?
Question by:GohdanTheMoblin
    LVL 5

    Accepted Solution

    To me this way looks good, but you can use also the ErrorProvider control. Exception are used to catch any kind of error on the execution of code and a bad formed string is a kind of error in the execution of the code.

    LVL 2

    Author Comment

    Yeah, I thought as much.  It'd be nice if there was a TryParse method.  The exception provides poor performance and I'd like a different way, but I guess it's all there is.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now