Results 1 to 3 of 3

Thread: Search for specific wording in text file and print words that follow

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    1

    Question Search for specific wording in text file and print words that follow

    I am somewhat new to VB and trying to figure out how to accomplish my task. And who better to ask then the people who know.

    What I am trying to accomplish is to search a txt file and locate a specific string and then assign the words that follow into labels. Here is a sample of the txt file.

    HW Ref Point Name Device Type Point Type Custom Label
    2-1 M1-1-0 PHOTO SMOKE ELEVATOR 3 PIT M1-1
    2-2 M1-2-0 HEAT HEAT ELEVATOR 3 PIT M1-2
    2-3 M1-3-0 PHOTO SMOKE ELEVATOR 4 PIT M1-3
    2-4 M1-4-0 HEAT HEAT ELEVATOR 4 PIT M1-4
    2-5 M1-5-0 HEAT HEAT ELEVATOR 1 PIT M1-5
    2-6 M1-6-0 PHOTO SMOKE ELEVATOR 1 PIT M1-6
    2-7 M1-7-0 HEAT HEAT ELEVATOR 2 PIT M1-7
    2-8 M1-8-0 PHOTO SMOKE ELEVATOR 2 PIT M1-8
    2-9 M1-9-0 ADRPUL PULL GFL CONCIERGE DESK M1-9

    In my form i have labels that will be assigned for each device type, point type and the custom label.

    Is there a way to search the text and locate M1-1-0 and then have it assign PHOTO to a label, lets say its named M1_1DT. Same for Point Type(M1_1PT) and the custom label(M1_1CL). I think once I understand how to accomplish this i should be able to repeat it to assign other text to their corresponding labels.

    My txt file is the output of another program so there is no way I know of to be able to easily put it into a database.

    Any help would be much appreciated, due to the fact that I'm pretty stumped on this. The most i have been able to do is to make a form be able to locate a string and display the number of times it is found in a message box. If this is not even the correct path feel free to let me know. Here is what i have so far to accomplish that.
    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim fileReader As String
            fileReader = My.Computer.FileSystem.ReadAllText("C:\preview1.txt")
            TextBox1.Text = fileReader
            Dim regex As New System.Text.RegularExpressions.Regex("       M1-1-0      ", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim regexMatches As MatchCollection = regex.Matches(fileReader)
            MessageBox.Show(regexMatches.Count)
        End Sub
    End Class
    Last edited by Hack; Dec 12th, 2013 at 06:07 PM. Reason: Added Code Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Search for specific wording in text file and print words that follow

    Moved From The Codebank (which is for sharing code rather than posting questions )

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Search for specific wording in text file and print words that follow

    I'm kinda struggling to see exactly what you want to do here as you seem to be wanting to isolate a single line but are using Regex match collections. Regex would appear to be overkill in this case anyway. As far as I can see you need to do nothing more than read the text to a string array using IO.File.ReadAllLines, identify the line you want using String methods such as .Contains and then Split the line on spaces to obtain each field value in a way that can be assigned to the appropriate label. Give that a go and let us know if you encounter any difficulties.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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