[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Find next non-whitespace character in VB.Net string array

Posted on 2006-11-24
4
Medium Priority
?
1,137 Views
Last Modified: 2012-05-05
In my VB.Net project I read in an entire text file and split the lines of the file into an array using split(FileContents, environment.newline())  

I now have to iterate through the lines array searching for keywords. Finding the keywords is easy (using IndexOf) but following each key word I need to read unknown strings that could be located on the same line as the keyword (separated from the keyword by spaces, tabs, etc) or on subsquent lines (possibly preceeded by tabs, spaces, etc.)

Here are some examples, all of which are legal syntax for my keyword/parameter sets:

  Keyword Parameter
  Keyword (tab) (tab) Parameter
  Keyword (space) (space) (space) Parameter
  Keyword (newline)
      (tab)(tab) Parameter

Since I never know what the parameter string is going to be, I can't use IndexOf() to find them. What I need to be able to do is find the next non-whitespace character following the keyword and then read characters until the next whitespace character and consider everything in-between to be the Parameter string.

So, what's the easiest way of doing this? I suspect there's a way to do it using regular expressions, but I have never used them before in .Net and I'm having a hard time finding examples online that do what I'm trying to do here.

Many, many thanks!
0
Comment
Question by:skwerm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 18009293
       Dim s(10) As String
        Dim sKeyword As String
        Dim sParam As String

after you find the keyword

            'erase the spaces
            sParam = System.Text.RegularExpressions.Regex.Replace(s(i), " ", "")
            'erase the tabs
            sParam = System.Text.RegularExpressions.Regex.Replace(sParam, vbTab, "")
            'erase the keyword...leaving only the parameter and this point
            sParam = System.Text.RegularExpressions.Regex.Replace(sParam, sKeyword, "")

You can give this a run to see how you can apply it (it prints output to the Immediate Window. original array first, then the keywords and associated parameters):

        Dim s(10) As String
        Dim sKeyword(10) As String
        Dim sParam(10) As String

        Dim i As Integer

        'making a sample array that is filled with different keywords (asdf + a number),
        'parameters (a number), spaces and tabs
        For i = 0 To 10
            s(i) = "Keyword" + i.ToString + vbTab + "          " + i.ToString
        Next

        'print the array
        For i = 0 To 10
            Debug.WriteLine(s(i))
        Next

        'not to be taken literally, but to show you how you can use Regex to isolate a parameter

        i = s.GetUpperBound(0) 'size of array

        'goes through the array, putting keywords and parameters in arrays...you could do a 2D array
        'uncomment msgbox to see it actually isolating the parameter
        Do While (i >= 0)

            sKeyword(i) = "Keyword" + i.ToString 'sample keyword
            sParam(i) = System.Text.RegularExpressions.Regex.Replace(s(i), " ", "")
            ' MsgBox(sParam(i))
            sParam(i) = System.Text.RegularExpressions.Regex.Replace(sParam(i), vbTab, "")
            'MsgBox(sParam(i))
            sParam(i) = System.Text.RegularExpressions.Regex.Replace(sParam(i), sKeyword(i), "")
            'MsgBox(sParam(i))

            i = i - 1
        Loop

        'show the parameters in the Immediate window
        For i = 0 To 10
            Debug.WriteLine(sKeyword(i) & ": " & sParam(i))
        Next
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 18010552
Hi skwerm;

Here is a sample code to do what you want.

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Read your file into a string variable
        Dim sr As New StreamReader("C:\Temp\KeywordParameter.txt")
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        ' Get an array of paramters used with the keyword
        Dim Param() As String = FindParameters(input, "Keyword")

        ' The Param array will now hold the list of all the parameters using the keyword

    End Sub

    Private Function FindParameters(ByVal Input As String, _
        ByVal Keyword As String) As String()

        Dim Output() As String = Nothing

        If Input Is Nothing OrElse Input = String.Empty OrElse _
            Keyword Is Nothing OrElse Keyword = String.Empty Then
            MessageBox.Show("One or both parmeters is invalid")
            Return Output
        End If


        Keyword = Regex.Escape(Keyword) & "\s+([^\s]+)"
        ' If you are using VS .Net before version 2005 comment the above line out
        ' and uncomment the following statement.
        'Keyword = Regex.Replace(Keyword, "([\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\])", _
        '    "\$1") & "\s+([^\s]+)"
        Dim mc As MatchCollection = Regex.Matches(Input, _
            Keyword, RegexOptions.IgnoreCase Or RegexOptions.Multiline)

        If mc.Count > 0 Then
            Dim BuildOutput As String = String.Empty
            For Each m As Match In mc
                BuildOutput += m.Groups(1).Value & "|"
            Next
            BuildOutput = BuildOutput.Remove(BuildOutput.LastIndexOf("|"c))
            Output = BuildOutput.Split("|"c)
        End If

        Return Output

    End Function
End Class

Fernando
0
 

Author Comment

by:skwerm
ID: 18012127
Thank you both for your responses!

YoungBonzi's method looks like it may work, but FernandoSoto's seems more straight-forward and (possibly) more efficient. I'm going to spend some time picking apart the regular expression provided and see if I can get the gist of it.

Thanks, again -- Both of you!

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18012611
Not a problem, glad I was able to help. ;=)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question