简体   繁体   中英

Regex to ignore file path drive letter colon

I have been testing this pattern:

Dim argumentsPattern As String = "/(?<var>.+):(?<val>.+)"

Which works fine when the string looks like:

/import-machinelist:Computers.txt

But when the string contains the file path, the second colon breaks the pattern.

/import-machinelist:C:\Users\Administrator\Desktop\WinZooka\WinZooka\bin\Debug\computers.txt

What can I do to fix the pattern to ignore the second colon ?

Here is the vb.net code i am using it in.

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

Any help would be appreciated.

You can use negative lookbehind which matches the colon only if it is not followed by \\ :

\/(?<var>.+):(?!\\)(?<val>.+)

Not sure it covers all your cases though.

Try changing the delimiter to a semicolon ( ; ) and then you can either use a regular expression or split the argument, like this:

Regular expression with semicolon:

Dim argumentsPattern As String = "/(?<var>.+);(?<val>.+)"

Split argument string by semicolon:

Dim argSplit As String() = arg.Split(';')

Select Case argSplit(0)
    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

You could make the first match-group non-greedy by adding a ? :

Dim argumentsPattern As String = "/(?<var>.+?):(?<val>.+)"

Which means it only captures up to just before the first colon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM