【发布时间】:2013-11-19 05:28:03
【问题描述】:
我一直在测试这种模式:
Dim argumentsPattern As String = "/(?<var>.+):(?<val>.+)"
当字符串看起来像这样时效果很好:
/import-machinelist:Computers.txt
但是当字符串包含文件路径时,第二个冒号会破坏模式。
/import-machinelist:C:\Users\Administrator\Desktop\WinZooka\WinZooka\bin\Debug\computers.txt
如何修复忽略第二个冒号的模式?
这是我正在使用的 vb.net 代码。
Dim commandLineArgs() As String = Environment.GetCommandLineArgs()
Dim argumentsPattern As String = "/(?<var>.+):(?<val>.+)"
Dim localRegex = New Regex(argumentsPattern, RegexOptions.IgnoreCase)
For Each arg As String In commandLineArgs
If arg = "/?" Then
MsgBox("Command line variables:" & Chr(13) & Chr(10) & _
"/username:JohnDoe" & Chr(13) & Chr(10) & _
"/password:Password1" & Chr(13) & Chr(10) & _
"/domain:lab.com" & Chr(13) & Chr(10) & _
"/import-machinelist:Computers.txt" & Chr(13) & Chr(10))
Else
Dim localMatch As Match = localRegex.Match(arg)
If localMatch.Success Then
Select Case localMatch.Groups("var").ToString
Case "username"
txtUser.Text = localMatch.Groups("val").ToString
Case "password"
txtPass.Text = localMatch.Groups("val").ToString
Case "domain"
txtDomain.Text = localMatch.Groups("val").ToString
Case "import-machinelist"
importMachineList(localMatch.Groups("val").ToString)
End Select
End If
End If
Next
任何帮助将不胜感激。
【问题讨论】:
-
将分隔符从 : 切换到 = 会更好吗?