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?

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.