5

ok I have the following strings

"^[a-z]*$"

and

"a-z"

now what I want with those two strings is to check if they are valid Regular Expressions strings in VB.NET. I really have no idea how can I make it... but I tried something below

Try
  Dim regex As Regex = New Regex("a-z")
  Return "valid regex"
Catch ex As Exception
 Return "not valid regex"
End Try

but my solution above seems not really good. Is there much better solution?

4

1 回答 1

7

No, there is no other solution (you could of course reimplement the regex parser, but that would be an error-prone hell of work).

I would prefer catching the specific ArgumentException that the Regex constructor throws if the regex is invalid other than just Exception.

于 2013-07-30T07:11:53.470 回答