regex 正则表达式函数提取子组中的数据

tyg4sfes  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(205)

我正在尝试将以下数据提取到www.example.com中的主组和子组vb.net
(abc())(def((123)(456))(klm((123))
预期结果
匹配1:abc
匹配2:def
Group 1 : 123
Group 2 : 456
匹配3:klm
Group 1 : 123
我已经尝试了下面的代码(\w+)\((\d+)\),但它不工作

6l7fqoea

6l7fqoea1#

看起来你想匹配一个“单词”后面加上括号内的数字。这里的通用方法将满足单词后括号内(num)数量未知的情况。
你可以用

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

并依赖于可在.NET regex API中访问的Match.Captures属性。以下是访问所有组和捕获值的方法:

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

参见this VB.NET demoregex demo。输出:

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

(\w+)\((?:\((\d+)\))*\)正则表达式匹配

  • (\w+)-第1组:一个或多个字符
  • \(- a (字符
  • (?:\((\d+)\))*-零次或多次重复
  • \(- a (字符
  • (\d+)-第2组:一个或多个数字
  • \)- a )字符
  • \)- a )字符
0mkxixxg

0mkxixxg2#

你可以使用这个正则表达式,它匹配前面有(的字母,然后使用向前看来捕获()中的一组或两组数字:

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

Demo on regex101
示例VB.Net代码:

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

输出:

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

Demo on ideone.com

相关问题