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
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
2条答案
按热度按时间6l7fqoea1#
看起来你想匹配一个“单词”后面加上括号内的数字。这里的通用方法将满足单词后括号内
(num)
数量未知的情况。你可以用
并依赖于可在.NET regex API中访问的Match.Captures属性。以下是访问所有组和捕获值的方法:
参见this VB.NET demo和regex demo。输出:
(\w+)\((?:\((\d+)\))*\)
正则表达式匹配(\w+)
-第1组:一个或多个字符\(
- a(
字符(?:\((\d+)\))*
-零次或多次重复\(
- a(
字符(\d+)
-第2组:一个或多个数字\)
- a)
字符\)
- a)
字符0mkxixxg2#
你可以使用这个正则表达式,它匹配前面有
(
的字母,然后使用向前看来捕获()
中的一组或两组数字:Demo on regex101
示例VB.Net代码:
输出:
Demo on ideone.com