1

I am trying to extract the below data into Main group and sub groups in vb.net

(abc())(def((123)(456))(klm((123))

Result Expected
Match 1 : abc
Match 2 : def
Group 1 : 123
Group 2 : 456
Match 3 : klm
Group 1 : 123

I have tried the below code (\w+)\((\d+)\), but its not working

2 Answers 2

2

It seems you want to match a "word" followed with parenthesized numbers inside parentheses. A generic approach here would cater for scenarios with unknown amount of (num)s inside the parentheses after the word.

You can use

(\w+)\((?:\((\d+)\))*\)

and rely on the Match.Captures property that is accessible in the .NET regex API. Here is a way to access all the group and capture values:

Imports System
Imports System.Text.RegularExpressions

Public Class Test
    Public Shared Sub Main() 
        Dim pattern As String = "(\w+)\((?:\((\d+)\))*\)"
        Dim text As String = "(abc())(def((123)(456))(klm((123))"
        Dim matches As MatchCollection = System.Text.RegularExpressions.Regex.Matches(text, pattern)
        For Each m As Match In matches
            Console.WriteLine("Match: " & m.Groups(1).Value)
            For Each g As Capture in m.Groups(2).Captures
                Console.WriteLine("Capture: " & g.Value)
            Next
        Next
        
    End Sub
End Class

See this VB.NET demo and the regex demo. Output:

Match: abc
Match: def
Capture: 123
Capture: 456
Match: klm
Capture: 123

The (\w+)\((?:\((\d+)\))*\) regex matches

  • (\w+) - Group 1: one or more word chars
  • \( - a ( char
  • (?:\((\d+)\))* - zero or more repetitions of
    • \( - a ( char
    • (\d+) - Group 2: one or more digits
    • \) - a ) char
  • \) - a ) char
Sign up to request clarification or add additional context in comments.

Comments

2

You could use this regex, which matches letters preceded by a (, then uses a forward lookahead to capture either one or two sets of digits inside ():

(?<=\()[a-z]+(?=\((?:\((\d+)\))?(?:\((\d+)\))?\))

Demo on regex101

Sample VB.Net code:

Imports System
Imports System.Text.RegularExpressions

Public Class Test
    Public Shared Sub Main() 
        Dim i As Integer
        Dim pattern As String = "(?<=\()[a-z]+(?=\((?:\((\d+)\))?(?:\((\d+)\))?\))"
        Dim text As String = "(abc())(def((123)(456))(klm((123))"
        Dim matches As MatchCollection = System.Text.RegularExpressions.Regex.Matches(text, pattern)
        For Each m As Match In matches
            i = 0
            For Each g As Group in m.Groups
                If i = 0 Then
                    Console.WriteLine("Match: " & g.Value)
                ElseIf i > 0 and g.Value <> "" Then
                    Console.WriteLine("Group " & i & ": " & g.Value)
                End If
                i = i + 1
            Next
        Next
    End Sub
End Class

Output:

Match: abc
Match: def
Group 1: 123
Group 2: 456
Match: klm
Group 1: 123

Demo on ideone.com

Comments

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.