Solved

how do i get this info from this line please

Posted on 2004-10-27
134 Views
Last Modified: 2010-04-23
i have a line i want to get SMTP and Delivery from it... i have others so i really need it to read all after the word start..

10/24/2004 4:41:00 AM - {   137} START Delivery

or

10/24/2004 4:53:20 AM - {   138} START SMTP

thx
Johnny
aka Pern
0
Question by:Johnny
    12 Comments
     
    LVL 15

    Expert Comment

    by:Timbo87
    Dim s As String = "10/24/2004 4:41:00 AM - {   137} START Delivery"
    Console.WriteLine(s.Substring(s.IndexOf("START") + 6))

    Output:
    Delivery
    0
     

    Author Comment

    by:Johnny
    im trying to parse the string in this question...

    http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21174471.html

    i have tryed to aply your answer..and i dont not only understand how this works(mid and right/left was so simple compared to dang vb.net string manipulations....) im really confused...

    im trying to search for the start to get the protocal..insted of the way we did it in the posted question/answer...

    im lost!
    thx for any help

    Johnny
    aka Pern
    0
     
    LVL 6

    Expert Comment

    by:etmendz
    Can you be more specific on what you really want to do? Show sample input and show expected output. ;-p
    0
     

    Author Comment

    by:Johnny
    if you review this question:
    http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21174471.html

    the protocal parseing is wrong its on line

    10/24/2004 4:53:20 AM - Requested SMTP connection from 209.236.1.22 [209.236.1.22], ID=138

    it should be
    10/24/2004 4:53:20 AM - {   138} START SMTP

    i tryed to add/modify the line you provided, but i cant get it to work...
    the part that needs change is

                    'Protocol is between "Requested" And "Connection"
                    TmpStr = Str.Substring(Str.IndexOf("Requested ") + 10, Str.IndexOf("connection ") - Str.IndexOf("Requested ") - 10).Trim
                    ''TmpStr = Str.Substring(Str.IndexOf("START") + 6)
                    LoginDetail(Index).Login_Protocal = TmpStr


    but if i change it..the other items like ip go away too..plus the part of
                If Str.IndexOf(" Requested ") >= 0 AndAlso Str.IndexOf(" connection ") >= 0 Then
    is not the correct line..to look for... so im really lost other then what ive told you...

    if you noticed we fixed the end (time) by looking for the end value... now i need to fix the start for protocal... so it picks up delivery.

    theres a sample log and the way i showed the info gathered to the screen there too...
    so u can test this if you want...
    im really lost on how this works im old school still in vb6 mode...im trying to learn vb.net but im getting frustrated..

    thx for any help
    Johnny
    aka Pern

    0
     

    Author Comment

    by:Johnny
    i tryed to add/modify the line you provided, but i cant get it to work...

    should read

    i tryed to add/modify the line provided, but i cant get it to work...
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    OK so instead of getting the Protocol from the previous Line you want the protocol from this line

    10/24/2004 4:53:20 AM - {   138} START SMTP

    Use

    If Str.IndexOf(" START ") >= 0 then
            LoginDetail(Index).Login_Protocal=Str.SubString(Str.IndexOf(" START ") + 7)
    End if

    See IndexOf and SubString are simple Funcions

    IndexOf gives the Index of occurance of a particular string within a string
    so Str.IndexOf(" START ") will return 31 i.e the position of first Char of START
    So in order to pick SMTP all we need to do is get the SubString Starting from the last Char of START. So we get the position of the first Char using IndexOf then add 7 the length of the String START and extract till the end of the string

    Now this is for extarcting the values once you get the string. Another question arises where's this line in the whole log, From the Log file you sent me it occurs just before the line

    10/24/2004 4:53:20 AM - Requested SMTP connection from 209.236.1.22 [209.236.1.22], ID=138

    So what you do is Modify the first IF block as shown below

     If Str.IndexOf(" START ") >= 0 then '----> Line Changed
                    'Check if Structure array is empty (This Checking not required in other IF conditions)
                    If LoginDetail Is Nothing Then
                        Index = 0
                    Else
                        Index = LoginDetail.Length
                    End If
                    ReDim Preserve LoginDetail(Index)
                   '----> Line Added
                    LoginDetail(Index).Login_Protocal=Str.SubString(Str.IndexOf(" START ") + 7)
    "NOW READ THE NEXT LINE SO THAT THE FLOW GOES ON AS IT IS
                     Str = LineInput(FNo)

                    TmpArr = Str.Split("-")
                    'First Element Forms Date & Time
                    LoginDetail(Index).Login_Date_Time = TmpArr(0)
                    'ID will be After Keyword "ID="
                    LoginDetail(Index).Login_ID = TmpArr(1).Substring(TmpArr(1).IndexOf("ID=") + 3) '+3 is for skipping ID=
                    'WE ALREADY GOT PROTOCOL FROM PREVIOUS LINE SO REMOVE THE COMMENTED LINES
                    'Protocol is between "Requested" And "Connection"
                    'TmpStr = Str.Substring(Str.IndexOf("Requested ") + 10, Str.IndexOf("connection ") - Str.IndexOf("Requested ") - 10).Trim
                    'LoginDetail(Index).Login_Protocal = TmpStr

                    'IP is between "[" And "]"
                    TmpStr = Str.Substring(Str.IndexOf("[") + 1, Str.IndexOf("]") - Str.IndexOf("[") - 1)
                    LoginDetail(Index).Login_IP = TmpStr


    0
     
    LVL 6

    Expert Comment

    by:etmendz
    If you are sure about the protocol name to match (SMTP, POP3, IMAP, etc.), I suggest you use regular expressions. String processing and manipulation is very easy with regular expressions. For example, to match the protocols SMTP, POP3 and IMAP:

    Dim s As String = "10/24/2004 4:53:20 AM - Requested SMTP connection from 209.236.1.22 [209.236.1.22], ID=138"

     ' this regular expression pattern matches "SMTP or POP3 or IMAP" in a string
    Dim rx As New Regex("(SMTP|POP3|IMAP)")

    ' this command executes the regular expression pattern matching process against the string s
    Dim m As Match = rx.Match(s)

    ' store the extracted value to a variable
    Dim protocol As String
    If m.Success protocol = m.Value

    If the protocol variable is empty, then that means no protocol is matched in the string. It's that simple!

    The advantage of using regular expressions is that you match against the pattern you want to extract. This frees you from parsing based on positions in a string. In addition, regular expressions are more powerful, flexible and they help to make your code more concise.

    Have fun.
    0
     
    LVL 6

    Expert Comment

    by:etmendz
    I noticed that you and arif_eqbal have gone a long way working on this. The way I look at it, the solution would have been simpler with regular expressions.

    More or less, you've already worked out the rules based on how and when the protocol token starts and ends as you read through the log. That is good progress and you can still use those rules especially when you start to abstract and modularize the rules in your code.

    For everything else (pattern matching, string extraction and string manipulation), you can use regular expressions here and there. In some areas, arif_eqbal's IndexOf() may still be useful. In some areas, regular expressions should make the code and processing look simpler.

    The following becomes a re-useable function for you:

    Function GetMatchString(ByVal pattern As String, ByVal value As String) As String
     Dim rx As New Regex(pattern)
     Dim m As Match = rx.Match(value)
     Dim s As String = ""
     If m.Success s = m.Value
     GetMatchString = s
    End Function

    Then you can use this in your code:

    Dim s As String = "10/24/2004 4:53:20 AM - {   138} START SMTP"

    ' this regular expression matches "} START "
    If GetMatchString("\} START ", s) <> "" Then

     ' this regular expression matches "SMTP" or "Delivery"
     Dim protocol As String = GetMatchString("(SMTP|Delivery)", s)

     ' so if you're getting the hang of it, yes, the pipe "|" means "OR" in regular expression syntax
     ' TODO

    End If

    Have fun.
    0
     

    Author Comment

    by:Johnny
    ok ive worked on this for a few days now and i cant get delivery to show for anything...


    heres the code so far... its not readding any delivery lines

    maybe we should do this all over again???

    it parse pop3,imap, and stmp fine and it assigns the value delivery but does not get all the other info at all


    -------- code -----

       Private Sub ParseFile(Optional ByVal Fl As String = "C:\sample_log.txt")
            Dim FNo As Integer = FreeFile()
            Dim Str As String
            Dim Index As Integer
            Dim TmpStr As String
            Dim TmpArr() As String

            FileOpen(FNo, Fl, OpenMode.Input, OpenAccess.Read)
            While Not EOF(FNo)
                Str = LineInput(FNo)

                'ListBox3.Items.Add("***********************") 'sep
                ListBox3.Items.Add(Str) 'sep
                ListBox3.Items.Add("***********************") 'sep

                'Then you can use this in your code:
                Dim s As String = Str
                ' this regular expression matches "} START "
                If GetMatchString("START", s) <> "" Then
                    ' this regular expression matches "SMTP" or "Delivery"
                    'Dim protocol As String = GetMatchString("(SMTP|Delivery)", s)
                    TmpStr = GetMatchString("(Delivery|SMTP|POP3|IMAP)", s)
                    ListBox3.Items.Add(TmpStr) 'STRING SEARCH
                    ListBox3.Items.Add("PROTOCAL ***********************") 'sep

                    'TmpStr = Str.Substring(Str.IndexOf("{ START ") + 41)
                    ' so if you're getting the hang of it, yes, the pipe "|" means "OR" in regular expression syntax
                End If

                If Str.IndexOf(" Requested ") >= 0 AndAlso Str.IndexOf(" connection ") >= 0 Then
                    'Check For Connection Request
                    'Check if Structure array is empty (This Checking not required in other IF conditions)
                    If LoginDetail Is Nothing Then
                        Index = 0
                    Else
                        Index = LoginDetail.Length
                    End If
                    ReDim Preserve LoginDetail(Index)

                    'Splitting on "-" will be easier for this String
                    TmpArr = Str.Split("-")
                    'First Element Forms Date & Time
                    LoginDetail(Index).Login_Date_Time = TmpArr(0)
                    'ID will be After Keyword "ID="
                    LoginDetail(Index).Login_ID = TmpArr(1).Substring(TmpArr(1).IndexOf("ID=") + 3) '+3 is for skipping ID=
                    'Protocol is between "Requested" And "Connection"
                    'TmpStr = Str.Substring(Str.IndexOf("Requested ") + 10, Str.IndexOf("connection ") - Str.IndexOf("Requested ") - 10).Trim
                    ''TmpStr = Str.Substring(Str.IndexOf("START") + 6)
                    LoginDetail(Index).Login_Protocal = TmpStr

                    'IP is between "[" And "]"
                    TmpStr = Str.Substring(Str.IndexOf("[") + 1, Str.IndexOf("]") - Str.IndexOf("[") - 1)
                    LoginDetail(Index).Login_IP = TmpStr

                ElseIf Str.IndexOf(" LOGIN ") >= 0 AndAlso Str.IndexOf("@") >= 0 Then
                    Str = Str.Substring(Str.IndexOf(" - "))
                    'Get the Login ID for which we are extracting Mail ID
                    'Since Login IDs are interlaced we need to be sure that the Mail ID is set to the correct Login ID So a separate function for it
                    TmpStr = Str.Substring(Str.IndexOf("(") + 1, Str.IndexOf(")") - Str.IndexOf("(") - 1).Trim
                    TmpArr = Str.Split("""")
                    UpdateMailID(TmpStr, TmpArr(1))

                    'ElseIf Str.IndexOf(" LOGOUT ") >= 0 AndAlso Str.IndexOf("completed") >= 0 Then
                ElseIf Str.IndexOf(" END ") >= 0 Then
                    TmpArr = Str.Split("-")
                    'Get ?? ID
                    TmpStr = Str.Substring(Str.IndexOf("{") + 1, Str.IndexOf("}") - Str.IndexOf("{") - 1).Trim '---> The brackets have changed ( to {
                    'First Element Forms Date & Time of Logout
                    UpdateLogout(TmpStr, TmpArr(0))
                End If
            End While
            FileClose(FNo)

        End Sub
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    Hi Pern

    You have made the changes right but it won't work though
    You see as I mentioned earlier Requests are not in a sequence, What I mean is When say IMAP request comes from 757 before he logs out another request comes from 758

    So before we can get the logout time of ID 757 we get the Details like IP,Protocol etc. of 758. So there's a mix up. The result of this is you can't use ListBox.Items.Add because the sequence is not followed and it will mix things up.

    That is why I used a Structure Array, and the functions UpdateLogout, UpdateMailID are required for that reason only. Because when we get Mail ID we can not be sure if it is for 757 or 758, So these functions search and put the value at appropriate place

    Now since you want the whole thing displayed in a List what you can do is after the ile is parsed write a function to Add values to the list

    I am sending a re-written code for you, also I have used Indexof functions only and not Regular Expressions because I will have to re-write the things otherwise

    0
     
    LVL 19

    Accepted Solution

    by:
     Private Sub ParseFile(Optional ByVal Fl As String = "C:\sample_log.txt")

            Dim FNo As Integer = FreeFile()
            Dim Str As String
            Dim Index As Integer
            Dim TmpStr As String
            Dim TmpArr() As String

            FileOpen(FNo, Fl, OpenMode.Input, OpenAccess.Read)
            While Not EOF(FNo)
                Str = LineInput(FNo)
                'Check For Connection Request
                If Str.IndexOf(" START ") >= 0 Then
                    'Check if Structure array is empty (This Checking not required in other IF conditions)
                    If LoginDetail Is Nothing Then
                        Index = 0
                    Else
                        Index = LoginDetail.Length
                    End If
                    ReDim Preserve LoginDetail(Index)
                    LoginDetail(Index).Login_Protocal = Str.Substring(Str.IndexOf(" START ") + 7)
                    Str = LineInput(FNo)
                End If
                If Str.IndexOf(" Requested ") >= 0 AndAlso Str.IndexOf(" connection ") >= 0 Then

                    'Splitting on "-" will be easier for this String
                    TmpArr = Str.Split("-")
                    'First Element Forms Date & Time
                    LoginDetail(Index).Login_Date_Time = TmpArr(0)
                    'ID will be After Keyword "ID="
                    LoginDetail(Index).Login_ID = TmpArr(1).Substring(TmpArr(1).IndexOf("ID=") + 3) '+3 is for skipping ID=
                    'Protocol is between "Requested" And "Connection"
                    'We have already taken the protocol so no need here
                    'REMOVE NEXT TWO LINES, I HAVE COMMENTED THEM
                    'TmpStr = Str.Substring(Str.IndexOf("Requested ") + 10, Str.IndexOf("connection ") - Str.IndexOf("Requested ") - 10).Trim
                    'LoginDetail(Index).Login_Protocal = TmpStr

                    'IP is between "[" And "]"
                    TmpStr = Str.Substring(Str.IndexOf("[") + 1, Str.IndexOf("]") - Str.IndexOf("[") - 1)
                    LoginDetail(Index).Login_IP = TmpStr

                ElseIf Str.IndexOf(" LOGIN ") >= 0 AndAlso Str.IndexOf("@") >= 0 Then
                    Str = Str.Substring(Str.IndexOf(" - "))
                    'Get the Login ID for which we are extracting Mail ID
                    'Since Login IDs are interlaced we need to be sure that the Mail ID is set to the correct Login ID So a separate function for it
                    TmpStr = Str.Substring(Str.IndexOf("(") + 1, Str.IndexOf(")") - Str.IndexOf("(") - 1).Trim
                    TmpArr = Str.Split("""")
                    UpdateMailID(TmpStr, TmpArr(1))

                ElseIf Str.IndexOf(" LOGOUT ") >= 0 AndAlso Str.IndexOf("completed") >= 0 Then
                    TmpArr = Str.Split("-")
                    'Get ID
                    TmpStr = Str.Substring(Str.IndexOf("(") + 1, Str.IndexOf(")") - Str.IndexOf("(") - 1).Trim
                    'First Element Forms Date & Time of Logout
                    UpdateLogout(TmpStr, TmpArr(0))
                End If
            End While
            FileClose(FNo)
            'FILL THE LISTBOX
            FillList()

        End Sub


        Private Sub FillList()

            Dim i As Integer
            For i = 0 To UBound(LoginDetail) - 1
                ListBox1.Items.Add("ID " & LoginDetail(i).Login_ID)
                ListBox1.Items.Add("Login Time " & LoginDetail(i).Login_Date_Time)
                ListBox1.Items.Add("Email " & LoginDetail(i).Login_Email)
                ListBox1.Items.Add("Protocol " & LoginDetail(i).Login_Protocal)
                ListBox1.Items.Add("IP " & LoginDetail(i).Login_IP)
                ListBox1.Items.Add("Logout Time " & LoginDetail(i).Logoff_Date_Time)
                ListBox1.Items.Add("--------------------")
            Next
        End Sub
        Private Sub UpdateMailID(ByVal LoginID As String, ByVal MailID As String)
            'These Two functions search based on the Login ID and update the MailID or Logout Time
            'This is because the Index might be pointing to some other user who logged in before
            'this guy logged out or provided his mail ID
            Dim i As Integer
            For i = 0 To LoginDetail.Length - 1
                If LoginDetail(i).Login_ID = LoginID Then
                    LoginDetail(i).Login_Email = MailID
                    Exit Sub
                End If
            Next
            'Error if execution comes here: Login ID and Mail ID do not match (Should Not happen)
        End Sub

        Private Sub UpdateLogout(ByVal LoginID As String, ByVal LogoutDateTime As String)
            Dim i As Integer
            For i = 0 To LoginDetail.Length - 1
                If LoginDetail(i).Login_ID = LoginID Then
                    LoginDetail(i).Logoff_Date_Time = LogoutDateTime
                    Exit Sub
                End If
            Next
            'Error if execution comes here: Login ID and Logout ID do not match (Should Not happen)
        End Sub

    0
     

    Author Comment

    by:Johnny
    thx
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now