1

I have a string representing an object property:

Dim path = "Person.AddressHistory(0).Street1"

and I am splitting it using path.Split("."C). Then I am iterating through it using a For-Each loop. I want to check if any of the "path sections" (or property names), such as AddressHistory(0) contains parentheses and an index value, then I wish to extract the index value (in this case the integer 0).

I will then eventually be able to use this technique to find the value of the last path section, i.e. Street1 (or any value pointed to by a given path).

I don't know much about visual basic regex or string parsing though. So far I have this:

Private Function GetValue(root As Object, path As String) As Object

    Dim pathSections = path.Split("."C)

    For Each section In pathSections

        Dim index As Integer
        Dim info As System.Reflection.PropertyInfo

        If section.Contains("(%d)") Then
            'todo: get index...
            'index = section.<Get index using regex>() 
        End If

        ' reflection to get next property value
        ' root = <get next value...>
    Next

    Return root

End Function

1 Answer 1

1

To match a section only consisting of word chars with 1+ digits inside (...) at the end, you may use

^\w+\(([0-9]+)\)$

See the regex demo. Then get the match.Groups(1).Value.

If there is no match, there are no digit(s) inside parentheses at the end of the string.

See a demo of this approach:

Dim path As String = "Person.AddressHistory(0).Street1"
Dim rx As Regex = New Regex("^\w+\(([0-9]+)\)$")
Dim pathSections() As String = path.Split("."c)
Dim section As String
For Each section In pathSections
    Dim my_result As Match = rx.Match(section)
    If my_result.Success Then
        Console.WriteLine("{0} contains '{1}'", section, my_result.Groups(1).Value)
    Else
        Console.WriteLine("{0}  does not contain (digits) at the end", section)
    End If
Next

Result:

Person  does not contain (digits) at the end
AddressHistory(0) contains '0'
Street1  does not contain (digits) at the end

Note that capturing group numbering starts with 1 as the Group 0 is the whole match. It means that match.Groups(0).Value = match.Value. So, in this case, AddressHistory(0) is the match.Groups(0).Value and 0 is the match.Groups(1).Value.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I switched to using Regex.Match(section,"^\w+\(([0-9]+)\)$") to retrieve a Match object. Does the groups index for match.Groups(1).Value start at 0 or 1? It's confusing me a bit.
@Mayron I added a demo and some explanation on capturing group numbering.
Thank you very much! I was using the shared function "Match" from class Regex instead of creating an instance. Not sure if that makes any difference but either way I'll use your example as it works perfectly.
Well, since you are using the same regex inside a loop, it makes sense to build the regex object, but since the regex is actually cached after its first usage, you may really use Regex.Match(s, PatternAsString).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.