?
Solved

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

Posted on 2006-11-24
4
Medium Priority
?
1,122 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 63

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 63

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

762 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