4 Replies - 2469 Views - Last Post: 06 October 2012 - 07:25 AM Rate Topic: -----

#1 hossamezz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

Read Specific words from txt file

Posted 05 October 2012 - 03:18 PM

I have txt file (xdos.txt) and this is its contents:
-----------------------------------------------------------
Pinging yahoo.com [98.139.183.24] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Reply from 98.139.183.24: bytes=32 time=251ms TTL=51

Ping statistics for 98.139.183.24:
Packets: Sent = 4, Received = 1, Lost = 3 (75% loss),
Approximate round trip times in milli-seconds:
Minimum = 251ms, Maximum = 251ms, Average = 251ms
------------------------------------------------------------

I want to read only "from 98.139.183.24" from the file and put them in textbox1.text

Is This A Good Question/Topic? 0
  • +

Replies To: Read Specific words from txt file

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Read Specific words from txt file

Posted 05 October 2012 - 03:38 PM

Have a look at SubString, Contains, and IndexOf, all of which are methods of the String class.

Let us know if you need more hints.
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: Read Specific words from txt file

Posted 05 October 2012 - 03:41 PM

does the .txt file always contain this structure ?

why dont you read one line at time at the correct position add this to a text box

kevin
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Read Specific words from txt file

Posted 05 October 2012 - 11:17 PM

Using Regex class (don't forget to import System.Text.RegularExpressions namespace) will really do the work for you.

You can do it the way lar3ry mentioned, but learning regular expressions will save you a lot of time in similar situations. Instead of counting how many characters you need to subtract from string (and substring), and writing method to do all this, you simply match your string with given pattern:

Dim extractedTextResults As MatchCollection = Regex.Matches(myTestString, "(?<=Reply )(.*?)(?=: bytes)")

where myTestString is your text, from where you want the results matching everything between "Reply " and ": bytes", excluding those two. What is left is "from XX.XXX.XXX.XX" in your case (XX represents any IP).

You than iterate through this MatchCollection. For example:
        For Each item In extractedTextResults
            Console.WriteLine("Extracted result: {0}{1}", item.ToString, vbNewLine)
        Next


Was This Post Helpful? 1
  • +
  • -

#5 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: Read Specific words from txt file

Posted 06 October 2012 - 07:25 AM

Using String.Split Method:
        Dim strRead As String = IO.File.ReadAllText("xdos.txt")
        Dim ip As String = strRead.Split(New String() {" from "}, StringSplitOptions.None)(1).Split(":")(0)
        Console.WriteLine(ip)
        Console.ReadKey()



Outputs the ip from the file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1