I know this is quite simple for you to code but for me as a beginner I need some examples or guides.

I have a text file which has this content:

random line
random line
random line
random line
random line
random line
random line
random27061-1-0256487952random words random words
random line

I need to find this: 27061-1-0256487952 in that file and write it on the other file, is there a way I can do this? The numbers is not always in that length, sometimes it's like this: 2 7 0 6 1 - 1 - 0 2 5 6 4 8 7 9 5 2 or this: 27 06 1-1-02564 8795 2. Is there a way? Thanks in advance?

Recommended Answers

All 4 Replies

I would not suggest a RegEx if the target contents are ALWAYS the same value with random spacing. I would just remove all spaces from the input text and search for substring containment (Contains()).

hi thines01, thanks for the reply. can you give me some samples?

OK. I've changed my mind -- I would use a RegEx to remove anything but digits and then evaluate.

Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
   Sub Main()
      Dim fileIn As New StreamReader("..\..\TextFile1.txt")
      Dim strData As String = ""
      Dim lngCount As Long
      '
      While (Not (fileIn.EndOfStream))
         strData = Regex.Replace(fileIn.ReadLine(), "[^0-9]", "")
         lngCount = lngCount + 1
         If (strData.Equals("2706110256487952")) Then
            ' Got it
            Console.WriteLine("Found on line {0}: {1}", lngCount, strData)
         End If
      End While
      fileIn.Close()
   End Sub
End Module

thanks a lot thines01.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.