, , MatchedString . , :
Public Class MatchedString
Public Enum InvalidValueBehaviors
SetToEmpty
AllowSetToInvalidValue
DoNothing
End Enum
Public Sub New(ByVal Expression As String)
Me.expression = Expression
exp = New Regex(Me.expression)
End Sub
Public Sub New(ByVal Description As String, ByVal Expression As String)
Me.expression = Expression
exp = New Regex(Me.expression)
_expressiondescription = Description
End Sub
Public Sub New(ByVal Expression As String, ByVal ThrowOnInvalidValue As Boolean, ByVal InvalidValueBehavior As InvalidValueBehaviors)
Me.expression = Expression
exp = New Regex(Me.expression)
Me.ThrowOnInvalidValue = ThrowOnInvalidValue
Me.InvalidValueBehavior = InvalidValueBehavior
End Sub
Public Sub New(ByVal Description As String, ByVal Expression As String, ByVal ThrowOnInvalidValue As Boolean, ByVal InvalidValueBehavior As InvalidValueBehaviors)
Me.expression = Expression
exp = New Regex(Me.expression)
_expressiondescription = Description
Me.ThrowOnInvalidValue = ThrowOnInvalidValue
Me.InvalidValueBehavior = InvalidValueBehavior
End Sub
Private exp As Regex
Private expression As String
Public ReadOnly Property MatchExpression() As String
Get
Return expression
End Get
End Property
Public ReadOnly Property ExpressionDescription() As String
Get
Return _expressiondescription
End Get
End Property
Private _expressiondescription As String
Public Function CheckIsMatch(ByVal s As String)
Return exp.IsMatch(s)
End Function
Public Property ThrowOnInvalidValue() As Boolean
Get
Return _thrownoninvalidvalue
End Get
Set(ByVal value As Boolean)
_thrownoninvalidvalue = value
End Set
End Property
Private _thrownoninvalidvalue = True
Public Property InvalidValueBehavior() As InvalidValueBehaviors
Get
Return _invalidvaluebehavior
End Get
Set(ByVal value As InvalidValueBehaviors)
_invalidvaluebehavior = value
End Set
End Property
Private _invalidvaluebehavior As InvalidValueBehaviors = InvalidValueBehaviors.DoNothing
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
If value Is Nothing Then value = "" 'Never set to Nothing
If CheckIsMatch(value) Then
_value = value
Else
Select Case InvalidValueBehavior
Case InvalidValueBehaviors.AllowSetToInvalidValue
_value = value
Case InvalidValueBehaviors.SetToEmpty
_value = ""
End Select
If ThrowOnInvalidValue Then
Throw New ArgumentOutOfRangeException(String.Format("String: {0} does not match expression: {1}", value, MatchExpression))
End If
End If
End Set
End Property
Private _value As String = ""
Public Overrides Function ToString() As String
Return _value
End Function
End Class