2017-06-04 8 views
0

a-z A-Z spacesが入力できたかどうかを調べるには、以下の正規表現は正しいですか?正規表現の入力が許可されています

Dim pattern As String = "^([a-zA-Z0-9 ]+\s)*[a-zA-Z0-9 ]+$" 
    Dim r As New Regex(pattern) 

    If Not r.IsMatch(TextBoxX1.Text) Then 
     MsgBox("not allowed") 
    End If 

答えて

0

は、私の知る限りでは、それはあなたが必要とするものありませんが、それは本当に不要な表現です。各文字がA〜Z、0〜9または空白であるかどうかを調べるのではなく、のうち、でないものを探します。

Dim pattern As String = "[^A-Z0-9 ]" 
Dim r As New Regex(pattern, RegexOptions.IgnoreCase) 

If r.IsMatch(TextBoxX1.Text) Then 
    MessageBox.Show("Invalid input!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
End If 

注:試合は大文字と小文字を区別しないあるので、私はA-Zを指定していてもそれは同様a-zと一致しますRegexOptions.IgnoreCaseを指定します。

オンラインテスト:https://dotnetfiddle.net/Q23XiA

パターンの説明:

[^    => Match any character that IS NOT... 
    A-Z0-9  => ...A-Z, 0-9 or a space (there's a space on the end of that). 
]    => End of character group. 
関連する問題