954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regex I can't figure it out, kindly help

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?

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

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()).

thines01
Postaholic
Team Colleague
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

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
thines01
Postaholic
Team Colleague
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

thanks a lot thines01.

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You