在这里,您有一个非基于正则表达式的解决方案。
Dim inputString As String = "my field"
If (fieldIsValid(inputString)) Then
'Do anything
End If
fieldIsValid
函数定义在哪里:
Private Function fieldIsValid(inputString As String) As Boolean
Dim delimiters() As String = New String() {" ", "!", """", "£", "$", "%", "^"}
If StringContains(inputString, delimiters) Then
Return False
End If
If (IsNumeric(inputString.Substring(0, 1)) OrElse inputString.Substring(0, 1) = "@") Then
Return False
End If
Return True
End Function
Private Function StringContains(ByVal inputString As String, ByVal ParamArray delimiters() As String) As Boolean
For Each delimiter In delimiters
If inputString.Contains(delimiter) Then
Return True
End If
Next
Return False
End Function
如您所见,这次与基于正则表达式的解决方案的代码大小差异并不算大;如果您想在将来改进/扩展它,这种解决方案也提供了更大的灵活性。关于“特殊字符”,您必须更具体;即使在使用正则表达式的情况下,您也必须说出您认为的特殊字符。