これについてはどうですか:
(?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))
これは、出力として:
(0)<0>: (?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))
(1)<x>: \b[a-s03]+\b
(2)<a>: \p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))
(3)<b>: .+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30)))
(4)<c>: .+?;(.+?(?:\d|sample-text|(\k'x'|sos30))
(5)<5>: .+?(?:\d|sample-text|(\k'x'|sos30)
(6)<6>: \k'x'|sos30
これはコードです:
Imports System.Collections.Specialized
Module Module1
Public DictGroups As New OrderedDictionary
Public DictTrackers As New Dictionary(Of Integer, Boolean)
Public intGroups As Integer = 0
Public CommandGroup As Boolean = False
Sub Main()
Dim regexToEval As String = "(?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))"
Dim curChar As String = ""
DictGroups.Add(0, "(0)<0>: " & vbTab)
DictTrackers.Add(0, True)
For i = 1 To regexToEval.Length
Dim iChar As String = regexToEval.Substring(i - 1, 1)
If curChar <> "\" AndAlso iChar = ")" Then EndGroup()
AddStrToTrackers(iChar)
If curChar = "\" OrElse iChar <> "(" OrElse regexToEval.Length < i + 2 Then curChar = iChar : Continue For
If regexToEval.Substring(i, 1) = "?" Then
i += 1 : AddStrToTrackers("?")
If regexToEval.Substring(i, 1) = ":" Then i += 1 : AddStrToTrackers(":") : curChar = ":" : Continue For
Dim NameLength As Integer = 0
If regexToEval.Substring(i, 1) = "<" Or regexToEval.Substring(i, 1) = "'" Then
i += 1 : AddStrToTrackers(regexToEval.Substring(i - 1, 1))
i += 1
For x = i To regexToEval.Length
If regexToEval.Substring(x - 1, 1) = ">" Or regexToEval.Substring(x - 1, 1) = "'" Then
NameLength = x - i
Exit For
End If
Next
Else
CommandGroup = True
Continue For
End If
If NameLength > 0 Then
Dim GroupName As String = regexToEval.Substring(i - 1, NameLength)
i += NameLength : curChar = regexToEval.Substring(i - 1, 1) : AddStrToTrackers(GroupName & curChar)
intGroups += 1
DictGroups.Add(intGroups, "(" & DictGroups.Count & ")<" & GroupName & ">: " & vbTab)
DictTrackers.Add(intGroups, True)
Continue For
End If
End If
curChar = iChar
intGroups += 1
DictGroups.Add(intGroups, "(" & DictGroups.Count & ")<" & intGroups.ToString & ">: " & vbTab)
DictTrackers.Add(intGroups, True)
Next
Dim Output As String = MakeOutput()
End Sub
Private Function MakeOutput() As String
Dim retString As String = String.Empty
For i = 0 To DictGroups.Count - 1
retString &= DictGroups(i) & vbCrLf
Next
Return retString
End Function
Public Sub EndGroup()
If CommandGroup Then
CommandGroup = False
Exit Sub
End If
Dim HighestNum As Integer = 0
For Each item In DictTrackers
If Not item.Value Then Continue For
If item.Key > HighestNum Then HighestNum = item.Key
Next
If HighestNum <> 0 Then DictTrackers(HighestNum) = False
End Sub
Public Sub AddStrToTrackers(ByVal addString As String)
For Each item In DictTrackers
If item.Value Then DictGroups(item.Key) &= addString
Next
End Sub
End Module
唯一の違いは、非キャプチャ グループも関数グループもキャプチャしていないことです。もちろん、これは私が 10 分ほどで作成した簡単なコードです。しかし、あなたがそれを望むなら、それは始まりです。Group-Numbers のキーとして OrderedDictionary を使用します。非キャプチャ グループと関数グループも出力に含めたい場合は、その構造を変更できます。