John Bergqvist
John Bergqvist

Reputation: 1003

How to capture value between two strings in VB.NET

i'm trying to capture a value between two strings using VB.NET

Each line from the file i'm reading in from can contain many different parameters, in any order, and I'd like to store the values of these parameters in their own variables. Two sample lines would be:

identifier="121" messagecount="112358" timestamp="11:31:41.622" column="5" row="98" colour="ORANGE" value="Hello"

or it could be:

identifier="1121" messagecount="1123488" timestamp="19:14:41.568" valid="true" state="running"

Also, this may not be the sole text in the string, there may be other values before and after (and in between) the parameters i would like to capture.

So essentially i'd need to store everything between 'identifier="' and it's closing '"' into an identifier variable, and so on... As the order of these parameters within each line can change, i can't simply stick the first value in one variable each time, I have to refer to them specifically by what their name is (identifier, messagecount) etc.

Can anyone help? Thanks. I guess it would be via a regular expression, but i'm not too hot on those. I'd prefer to have each expression for each paramater within it's own statement, rather than being all in one, thanks.

Upvotes: 0

Views: 824

Answers (2)

Grim
Grim

Reputation: 672

You just need to split the data into manageable clumps, and then go through it. Something like this to start you off.

Private Sub ProcessMyData(LineOfData As String)

    ' NOTE!  This assumes all your 'names' have no spaces in!
    Dim vElements = LineOfData.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)

    For Each vElement In vElements
        Dim vPair = vElement.Split({"="c})

        Dim vResult = vPair(1).Trim(Convert.ToChar(34))

        Select Case vPair(0).ToLower
            Case "identifier"
                MyIDVariable = CInt(vResult)

            Case "colour"
                MyColourVariable = vResult

            ' etc., etc.
        End Select
    Next
End Sub

You can define the variables you want locally in the sub [function], and then return a list/dictionary/custom class of the things you're interested in.

Upvotes: 1

KekuSemau
KekuSemau

Reputation: 6853

Here is a sample how you can go about that. It converts one line into a dictionary.

This will capture any string consisting of a-z-characters (case-insensitive) as the attribute name, and then catch any character other than " in the value string. (If " can occur in the string as "" you need to add some treatment for that.)

    Imports System.Text.RegularExpressions
    [...]

            Dim s As String = 
               "identifier=""121"" messagecount=""112358"" " &
               "timestamp=""11:31:41.622"" column=""5"" row=""98"" " &
               "colour=""ORANGE"" value=""Hello"""
            Dim d As New Dictionary(Of String, String)

            Dim rx As New Regex("([a-z]+)=""(.*?)""", RegexOptions.IgnoreCase)

            Dim rxM As MatchCollection = rx.Matches(s)
            For Each M As Match In rxM
                d.Add(M.Groups(1).Value, M.Groups(2).Value)
            Next

            ' Dictionary is ready
            ' test output
            For Each k As String In d.Keys
                MsgBox(String.Format("{0} => {1}", k, d(k)))
            Next

Upvotes: 1

Related Questions