我想将文本框的一部分提取到另一个文本框。所以我有 2 个文本框,即 txtuser 和 txtpassword。扫描二维码时,选择txtuser会自动将扫描结果分离为txt.user和txtpassword。
谢谢
'This is the result of the QR barcode scanner
USERNAME : TEST-PASSWORD : TEST@123
'so the result I want
txtuser.txt = TEST
txtPassword.txt = TEST@123
您可以使用正则表达式:
Dim input As String = "USERNAME : TEST-PASSWORD : TEST@123"
Dim pattern As String = "USERNAME : (\S+)-PASSWORD : (\S+)"
Dim m As Match = Regex.Match(input, pattern)
If m.Success Then
Dim user As String = m.Groups(1).Value
Dim password As String = m.Groups(2).Value
Console.WriteLine($"User = {user}, Password = {password}")
Else
Console.WriteLine("no match")
End If
控制台输出:
User = TEST, Password = TEST@123
正则表达式模式的解释
USERNAME : (\S+)-PASSWORD : (\S+)
:
(\S+)
捕获组 1 和 2 匹配至少一个非空白字符的用户名和密码。带有文本框:
Const pattern As String = "USERNAME : (\S+)-PASSWORD : (\S+)"
Dim m As Match = Regex.Match(txtUser.Text, pattern)
If m.Success Then
txtUser.Text = m.Groups(1).Value
txtPassword.Text = m.Groups(2).Value
Else
'TODO: display some message
End If