VB.Net:正则表达式

时间:2014-01-09 19:31:10

标签: regex vb.net

我正在创建一个应用程序,它可以告诉我谁在我工作的制造中心登录到哪台PC。

我正在使用psexec的psloggedon cmd进程来获取我和VB.net Windows应用程序的信息以向我显示信息。

首先,我首先查询我们当前处于活动状态的所有PC的数据库,并将数据转储到datagridview对象中。 (如下所示)

Private Sub Button(sender As System.Object, e As System.EventArgs) Handles btngetPC.Click  

    'GET AREAS FROM DATABASE
    Dim ds As New DataSet()
    Dim db As String = "QUERY STRING GOES HERE"

    'CONNECT TO DATABASE
    Using da As New SqlDataAdapter(db, MySQLConnection)
        da.Fill(ds, "MACHINE_NAME")
    End Using
    With datagridView1
        .DataSource = ds.Tables("MACHINE_NAME")
    End With

    'ADD COLUMN TO DATAGRIDVIEW
    datagridView1.Columns.Add("LOGGED_IN", "LOGGED_IN")
    MySQLConnection.Close()
End Sub

一旦我用我所有的活动PC填充了我的datagridview对象,我就可以使用机器名运行psloggedon cmd来获取登录的用户。我这样做是通过使用:

Private Sub execute(sender As Object, e As EventArgs) Handles bntExecuteCmd.Click

    'COUNT ENTRIES
    Dim RowCount As Integer = datagridView1.RowCount
    ''EXECUTE CMD
    For i = 0 To RowCount - 2
        'PERFORM PSLOGGEDON ROUTINE
        Dim Proc1 As New Process
        Proc1.StartInfo = New ProcessStartInfo("psloggedon")
        Proc1.StartInfo.Arguments = "-l \\" & datagridView1.Rows(i).Cells(1).Value & ""
        Proc1.StartInfo.RedirectStandardOutput = True
        Proc1.StartInfo.UseShellExecute = False
        Proc1.StartInfo.CreateNoWindow = True
        Proc1.Start()

        If Not Proc1.WaitForExit(300) Then
            Proc1.Kill()
        End If

        'INSERT RESULTS INTO LOGGEN_IN COLUMN
        Dim msg As String = Proc1.StandardOutput.ReadToEnd
        Dim idx As Integer = msg.LastIndexOf("\"c)
        Dim user As String = msg.Substring(idx + 1)
        Dim final As String = UCase(System.Text.RegularExpressions.Regex.Replace(user, "^ELP.*$", ""))
        datagridView1.Rows(i).Cells(2).Value = final
    Next
End Sub

最后,这是我的问题:
要获得员工姓名,我必须使用正则表达式,因为原始格式是不可接受的。

原始格式:
“连接到\ ELPSC171698的注册表......

用户在本地登录:           ECHOSTAR \ Jane.Doe“

申请后

格式:

'INSERT RESULTS INTO LOGGEN_IN COLUMN  

        Dim msg As String = Proc1.StandardOutput.ReadToEnd
        Dim idx As Integer = msg.LastIndexOf("\"c)
        Dim user As String = msg.Substring(idx + 1)
        Dim final As String = UCase(System.Text.RegularExpressions.Regex.Replace(user, "^ELP.*$", ""))
        datagridView1.Rows(i).Cells(2).Value = final  

“PAULA.RODRIGUEZ”

看起来不错吧?但是,当原始格式具有多个关联时,如下所示:

“连接到\ ELPSC173068的注册表......

用户在本地登录:           ECHOSTAR \ John.Doe           ECHOSTAR \ Ben.Doe“

我写的代码会让我成为这个列表中的最后一个人。在这种情况下,当我需要获得BEN.DOE时,我将获得JOHN.DOE。

现在的问题是:如何更改此代码:

'INSERT RESULTS INTO LOGGEN_IN COLUMN  

        Dim msg As String = Proc1.StandardOutput.ReadToEnd
        Dim idx As Integer = msg.LastIndexOf("\"c)
        Dim user As String = msg.Substring(idx + 1)
        Dim final As String = UCase(System.Text.RegularExpressions.Regex.Replace(user, "^ELP.*$", ""))
        datagridView1.Rows(i).Cells(2).Value = final  

让我成为第一个人,“JOHN.DOE”来自这里:

“连接到\ ELPSC173068的注册表......

用户在本地登录:           ECHOSTAR \ John.Doe           ECHOSTAR \ Ben.Doe“

我希望我的问题清楚明确,构建得很好。谢谢。

2 个答案:

答案 0 :(得分:0)

使用更具体的正则表达式和捕获组来获取多个项目。

    Dim mc As MatchCollection = Regex.Matches("Users logged on locally: ECHOSTAR\John.Doe ECHOSTAR\Ben.Doe", "[\t ]+[a-z_0-9]+\\(?<n>[a-z_\.0-9]+)(([\t ])|($))", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Multiline)
    For Each m As Match In mc
        Dim name As String = m.Groups("n").value
    Next

如果你只是想要第一个......那么。

If mc.Count >= 1 Then
    Dim name As String = mc(0).Groups("n").Value
End If

答案 1 :(得分:0)

更改

Dim idx As Integer = msg.LastIndexOf("\"c)

Dim idx As Integer = msg.IndexOf("\"c)

或者考虑一下:

假设您已经首先验证了字符串。

Dim user As String = msg.Split({"\"c, " "c}, StringSplitOptions.RemoveEmptyEntries)(1) _
                     .Replace("."c, " "c).ToUpper

要删除小数,只需删除.Replace("."c, " "c)