Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Advanced String Manipulation

  1. #1

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Resolved [RESOLVED] [2005] Advanced String Manipulation

    Hello, I am having trouble with a program that I am making. I have a txt file and I load it in as a string. The txt file contains something like this:

    www.vbforums.com.
    ------------------------------------------------------
    Record Name . . . . . : www.vbforums.com
    Record Type . . . . . : 1
    Time To Live . . . . : 85014
    Data Length . . . . . : 4
    Section . . . . . . . : Answer
    A (Host) Record . . . :
    63.236.73.220

    Record Name . . . . . : vbforums.com
    Record Type . . . . . : 2
    Time To Live . . . . : 85014
    Data Length . . . . . : 4
    Section . . . . . . . : Authority
    NS Record . . . . . :
    ns1.internet.com

    Record Name . . . . . : vbforums.com
    Record Type . . . . . : 2
    Time To Live . . . . : 85014
    Data Length . . . . . : 4
    Section . . . . . . . : Authority
    NS Record . . . . . :
    ns3.internet.com

    Record Name . . . . . : ns1.internet.com
    Record Type . . . . . : 1
    Time To Live . . . . : 85014
    Data Length . . . . . : 4
    Section . . . . . . . : Additional
    A (Host) Record . . . :
    63.236.72.133

    Record Name . . . . . : ns3.internet.com
    Record Type . . . . . : 1
    Time To Live . . . . : 85014
    Data Length . . . . . : 4
    Section . . . . . . . : Additional
    A (Host) Record . . . :
    63.236.72.135
    It is just a log from a cmd command "ipconfig /displaydns"

    I would like to be able to just get out any substring that ends with the following:


    .com
    .net
    .org
    .info
    .edu
    .de
    .biz
    .tv
    .us
    .co.uk
    .eu

    And then it will output this:

    www.vbforums.com
    www.vbforums.com
    vbforums.com
    ns1.internet.com
    vbforums.com
    ns3.internet.com
    ns1.internet.com
    ns3.internet.com
    How in the world would I do that?

    Thanks

  2. #2
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Advanced String Manipulation

    Well, you can split the file by line using the following:

    vb.net Code:
    1. Dim textFile as String() = IO.File.ReadAllLines(filename)

    To find out what a line ends with use the EndsWith method:

    vb.net Code:
    1. Dim s As String = "www.vbforums.com"
    2. If s.EndsWith(".com") Then
    3. ' It does
    4. Else
    5. ' It does not
    6. End If
    Prefix has no suffix, but suffix has a prefix.

  3. #3

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    Thanks a lot.

    If it is not always www.vbforums.com what would I make s equal to?

  4. #4
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Advanced String Manipulation

    My bad I wasn't thinking straight.

    When you read the file into the textFile variable (in my last post) then you would read through that like this:

    vb.net Code:
    1. Dim output As List(Of String)
    2.         For Each s As String In textFile
    3.             If s.EndsWith(".com") OrElse s.EndsWith(".net") Then
    4.                 ' Now we will grab the last part of the line
    5.                 output.Add(Split(s, Chr(32))(temp.Length - 1))
    6.             End If
    7.         Next

    That code is untested but should work. Output will contain all the addresses.
    Prefix has no suffix, but suffix has a prefix.

  5. #5

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    The following is my full code so far, but with it I get: "Index was outside the bounds of the array."

    vb.net Code:
    1. Dim i As Integer
    2.         Dim textFile As String() = IO.File.ReadAllLines("C:\dns.txt")
    3.         Dim output As List(Of String)
    4.         For Each s As String In File.ReadAllLines("C:\dns.txt")
    5.             If s.EndsWith(".com") OrElse s.EndsWith(".net") Then
    6.                 ' Now we will grab the last part of the line
    7.                 output.Add(Split(s, Chr(32))(textFile.Length - 1))
    8.             End If
    9.         Next
    10.         For i = 0 To output.Capacity
    11.             txtURLS.Text = output.Item(i) - 1 & vbNewLine
    12.         Next

  6. #6
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Advanced String Manipulation

    vb.net Code:
    1. For i = 0 To output.Capacity
    Should be:
    vb.net Code:
    1. For i = 0 To output.Capacity - 1
    You are splitting the file with this:
    vb.net Code:
    1. Dim textFile As String() = IO.File.ReadAllLines("C:\dns.txt")
    So instead of this:
    vb.net Code:
    1. For Each s As String In File.ReadAllLines("C:\dns.txt")
    Use:
    vb.net Code:
    1. For Each s As String In textFile
    Prefix has no suffix, but suffix has a prefix.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: [2005] Advanced String Manipulation

    This regular expression pattern will work:
    [a-zA-Z0-9\.]+\.(com|net|org|info|edu|de|biz|tv|us|co|uk|eu)

    Here is an example for you:
    VB Code:
    1. 'get the contents of the file
    2. Dim data As String = IO.File.ReadAllText("DNSLog.txt")
    3. 'get something to store the results in for display
    4. Dim result As New System.Text.StringBuilder
    5. 'create the pattern to match by
    6. Dim pattern As String = "[a-zA-Z0-9\.]+\.(com|net|org|info|edu|de|biz|tv|us|co|uk|eu)"
    7. 'loop through all matches and add them to the results to be displayed
    8. For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(data, pattern)
    9.       result.AppendFormat("{1}{0}", Environment.NewLine, m.Value)
    10. Next
    11. 'display the results
    12. MsgBox(result.ToString)
    Of course the length of the RegEx calls could be trimmed down a bit with an imports System.Text.RegularExpressions
    Last edited by Edneeis; Jun 10th, 2007 at 01:49 AM.

  8. #8

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [2005] Advanced String Manipulation

    Thanks a lot. Plus rep for you both

  9. #9

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [RESOLVED] [2005] Advanced String Manipulation

    Now I am having troubles deleting duplicate lines for some reason.

    I got this code for erasing duplicate lines, but it isn't working when I try modifying it for this application:

    vb.net Code:
    1. Dim Lines As New ArrayList
    2.         'put all the lines of the textfile into the arraylist
    3.         Lines.AddRange(IO.File.ReadAllLines("C:\dns.txt"))
    4.         'loop the arraylist in reverse because the IndexOf searches top to bottom
    5.         For i As Integer = Lines.Count - 1 To 0 Step -1
    6.             'if the return index is smaller than the index of the line we are
    7.             'searching for then we can remove that line because there is a duplicate
    8.             If Lines.IndexOf(Lines(i)) < i Then
    9.                 Lines.RemoveAt(i)
    10.             End If
    11.         Next

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: [RESOLVED] [2005] Advanced String Manipulation

    It is easiest if you never add the duplicate result:
    vb Code:
    1. 'get the contents of the file
    2.         Dim data As String = IO.File.ReadAllText("DNSLog.txt")
    3.         'get something to store the results in for display
    4.         Dim result As New System.Text.StringBuilder
    5.         'create the pattern to match by
    6.         Dim pattern As String = "[a-zA-Z0-9\.]+\.(com|net|org|info|edu|de|biz|tv|us|co|uk|eu)"
    7.         'loop through all matches and add them to the results to be displayed
    8.         For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(data, pattern)
    9.             'check if the result is already part of the results
    10.             If Not result.ToString.Contains(String.Format("{1}{0}", Environment.NewLine, m.Value)) Then
    11.                 'only add new result if it is not a duplicate
    12.                 result.AppendFormat("{1}{0}", Environment.NewLine, m.Value)
    13.             End If
    14.         Next
    15.         'display the results
    16.         MsgBox(result.ToString)

  11. #11

    Thread Starter
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [RESOLVED] [2005] Advanced String Manipulation

    Ok, thanks a lot again.

    This is just going to be a program that monitors what websites you are going on (for kids, students, ect...) Good parental/administration tool.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width