3

I want to know if exist any method in RegEx class to check if an expression have valid syntax.

I'm not meaning if the regex matches a string or something similar, then the "IsMatch" or "Success" methods does not help me.

To understand me, for example when using the RegEx.Match method with this expression it throws an exception because the expression have invalid syntax:

"\\\" 

(without the double quotes)

I've checked the regex class methods but I can't find any like a "tryparser".

Then to check if an expression have valid syntax I'm doing this:

Try
    Regex.Match(String.Empty, "\")
    Return True
Catch
    Return False
End Try

Just I want to know if I can simplify the code more than that by directly returning a value from a method from regex class or converting to boolean the result of a regex class method.

UPDATE:

I create the RegEx in execution time, does not help me external tools.

enter image description here

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 1
    Duplicate of http://stackoverflow.com/questions/1371408/how-to-validate-a-regular-expression – Taemyr Sep 11 '13 at 08:20

2 Answers2

5

Technically you can use the constructor of Regex...

Private Shared Function IsRegexValid(str As String) As Boolean
    Dim result As Boolean
    Try
        Dim rx as Regex = New Regex(str)
        result = True
    Catch ex As ArgumentException
        result = False
    End Try
    Return result
End Function

or a method that builds a Regex object or returns Nothing...

Private Shared Function TryBuildRegex(str As String) As Regex
    Dim result As Regex
    Try
        result = New Regex(str)
    Catch ex As ArgumentException
        result = Nothing
    End Try
    Return result
End Function

Then

Dim isvalid As Boolean = IsRegexValid("\")

or

Dim rx As Regex = TryBuildRegex("\")

If rx IsNot Nothing Then
End If
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Thanks but your first function doesnt works trying to use the regex constructor throws an exception 'cause you are trying to use the NEW constructor as first keyword inside a procedure... but even working you are storing all the new regex objects in memory untill you does not dispose them, but I understand what you are trying to do but basically is a try/catch then just I would know if my original code can be improved more, not extended and allocating objects in mem. thanks again – ElektroStudios Sep 11 '13 at 08:05
  • @ElektroHacker `Regex` isn't `IDisposable`, so you don't "dispose" them. The Garbage Collector will dispose them when you don't use them anymore. But yes, I create a `Regex` to test if the `Regex` can be created :-) – xanatos Sep 11 '13 at 08:24
  • If your question was "Is there a `Regex.IsExpressionValid(String)`?" then the response is "no". You have to build the Regex in some way (as the `Regex.Match` does, or as the `new Regex` does) and see if it works. – xanatos Sep 11 '13 at 08:25
  • Thankyou for the acclaration that is what I would know, and yes I were a little bit conused about the regex object what I would said is I need to execute too many times faster the RegEx try/catch so the GC does not do the work at the moment when is needed. sorry for my english – ElektroStudios Sep 11 '13 at 09:10
1

Unless you are creating your regular expressions dynamically, you could use a tool like Expresso or The Regulator.

They are both very good and useful, they will also help you build your expression or analyze an existing one.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193