密码和电子邮件模式的 Visual Basic "Like" 运算符
Visual Basic "Like" operator for password and email Pattern
我正在尝试使用 Like 运算符来确保字符串与模式相匹配。我知道使用 regex(正则表达式)更容易,但我只说我必须使用“like”运算符。
我该怎么办?任何帮助将不胜感激。
密码文本框应至少包含一个字符、一个数字和这些符号之一#%&*。
让我们保持简单,顺便说一句,这就是我在电子邮件模式方面取得的进展。
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox2.Text Like "*[@]*?.[a-z]*" Then
MsgBox("OK")
Else
MsgBox("NO GOOD")
End If
End Sub
你应该寻找正则表达式。这是来自 MSDN 的示例,用于在 VB.
中查找电子邮件地址
Function ValidateEmail(ByVal email As String) As Boolean
Dim emailRegex As New System.Text.RegularExpressions.Regex(
"^(?<user>[^@]+)@(?<host>.+)$")
Dim emailMatch As System.Text.RegularExpressions.Match =
emailRegex.Match(email)
Return emailMatch.Success
End Function
https://msdn.microsoft.com/en-us/library/vstudio/txk0hsah%28v=vs.100%29.aspx
VB 中有关正则表达式的更多信息:http://support2.microsoft.com/kb/818802
The password textbox should at least contain one character, one number
and one of these symbols #%&*.
根据您的要求,您既不需要正则表达式也不需要 Like
-operator,这样效率更高且可读性也更高:
Dim isValid = text.Length > 0 AndAlso
text.Any(AddressOf Char.IsDigit) AndAlso
text.Any(AddressOf "#%&".Contains)
The password textbox should at least contain one character, one number and one of these symbols #%&*.
使用 LIKE
运算符解决此问题的唯一方法是组合多个 LIKE
操作,因为没有像正则表达式那样的 OR 或 lookahead/lookbehind 的概念。
这是一个例子:
Dim strings = {"das6723&", "das6723", "#das6723",
"fsdfdfs", "f74/&g3", "232323",
"ABC37&28", "J**1", "j87#"}
For Each s in strings
Console.Write(s)
If s Like "*#*" AndAlso _
s Like "*[#%&*]*" AndAlso _
s Like "*[a-zA-Z]*" Then
Console.WriteLine(" : valid")
Else
Console.WriteLine(" : not valid")
End If
Next
我正在尝试使用 Like 运算符来确保字符串与模式相匹配。我知道使用 regex(正则表达式)更容易,但我只说我必须使用“like”运算符。 我该怎么办?任何帮助将不胜感激。
密码文本框应至少包含一个字符、一个数字和这些符号之一#%&*。
让我们保持简单,顺便说一句,这就是我在电子邮件模式方面取得的进展。
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox2.Text Like "*[@]*?.[a-z]*" Then
MsgBox("OK")
Else
MsgBox("NO GOOD")
End If
End Sub
你应该寻找正则表达式。这是来自 MSDN 的示例,用于在 VB.
中查找电子邮件地址Function ValidateEmail(ByVal email As String) As Boolean
Dim emailRegex As New System.Text.RegularExpressions.Regex(
"^(?<user>[^@]+)@(?<host>.+)$")
Dim emailMatch As System.Text.RegularExpressions.Match =
emailRegex.Match(email)
Return emailMatch.Success
End Function
https://msdn.microsoft.com/en-us/library/vstudio/txk0hsah%28v=vs.100%29.aspx
VB 中有关正则表达式的更多信息:http://support2.microsoft.com/kb/818802
The password textbox should at least contain one character, one number and one of these symbols #%&*.
根据您的要求,您既不需要正则表达式也不需要 Like
-operator,这样效率更高且可读性也更高:
Dim isValid = text.Length > 0 AndAlso
text.Any(AddressOf Char.IsDigit) AndAlso
text.Any(AddressOf "#%&".Contains)
The password textbox should at least contain one character, one number and one of these symbols #%&*.
使用 LIKE
运算符解决此问题的唯一方法是组合多个 LIKE
操作,因为没有像正则表达式那样的 OR 或 lookahead/lookbehind 的概念。
这是一个例子:
Dim strings = {"das6723&", "das6723", "#das6723",
"fsdfdfs", "f74/&g3", "232323",
"ABC37&28", "J**1", "j87#"}
For Each s in strings
Console.Write(s)
If s Like "*#*" AndAlso _
s Like "*[#%&*]*" AndAlso _
s Like "*[a-zA-Z]*" Then
Console.WriteLine(" : valid")
Else
Console.WriteLine(" : not valid")
End If
Next