tags:

views:

66

answers:

2

In a .NET application (written in C#) I have a UI with a list and a text box used to enter a filter string to filter what is displayed in the list. I refresh the filter as the user types.

I now want to allow use of regular expressions in the filter string, which poses a problem. As the user types the expression it may be invalid, for instance between the time that an opening bracket is typed and the time that the closing bracket is typed. Newing up a Regex with an invalid expression throws an exception and I want to prevent this. One way is to catch the exception but I was wondering if there is some way to check that the expression is a valid regular expression without actually trying to new up a Regex with it.

+1  A: 

I would have a special start character that the user types to indicate they are starting a regex, at that point you can stop the live processing of the typed text until a special ending character is typed, at which point you new up and apply the regex.

slugster
Totally agreed and probalby best practice. Just say; "For use of Regex, type `rxstart(YOUR REGEX HERE)rxend` and parse for `rxstart(` and `)rxend` whatever is in between is defined as regex and tested. Other idea: Give the user a checkbox to let him state "I'm now typing regex".
ApoY2k
+5  A: 

Just catch the exception. Given that this will be happening in response to user input there will be absolutely no significant performance penalty.

GraemeF
I'm just going to go this route.
Steve Crane