tags:

views:

87

answers:

1

Hello,

Does anyone know where I can come by a regex that matches any valid C# style regular expression? Is it even possible?

FYI, the reason I'm trying to do this is because I have a mini language which allows regular expressions as part of it's syntax and I cobbled together crummy regex to validate the mini-language statements but it fails incorrectly on some more complicated expressions. The mini-language syntax is defined through a combination of eBNF and regular expressions. I could do this 'validation' in C# but i think if this approach is possible, it would be cleanest and best separation of concerns.

Thanks, brian

+8  A: 

No, you can't. At least not generally. Regular expressions describe regular languages and those are characterized by the fact that they cannot contain arbitrarily nested expressions. So something like

(ab(?:cd)e(fg))

is already pretty much impossible to validate with regular expressions alone. While certain flavors of regular expressions allow recursive descent into the match (Perl, for example) or balanced capture groups which can emulate this to some extent it is definitely not the tool meant for this job and you shouldn't try to shoehorn it into one.

What you can do is just try to compile an expression you want to validate. .NET's regular expression engine will throw an exception if the pattern is invalid:

var compiledRegex = new Regex(someString);
Joey
This is was I was afraid of but if it's the only option then i guess I have to deal with it. Thanks for the clarification / explanation.
sweeney
Using exceptions to control the flow of the application isn't best practise, but writing a validator for regular expressions isn't trivial.
Phil
Well using the line "new Regex(someString);" will essentially validate the regex. The string being validated is technically user input, so trapping an exception on bad user input is probably the most reasonable way to deal with this. Right?
sweeney
@sweeney: I don't see a particular problem with it either. Phil is right that validation is still something as an unexpected error that might need to be trapped. If you're doing this in a tight loop then the fact that exceptions are expensive will certainly bite you but from a pragmatic standpoint it's probably the easiest solution you have.
Joey
no tight looping, it actually only gets used a few times per run of the app. thanks for the input, guys.
sweeney