これが解決策です。2 つの正規表現を使用しました。1 つは元の文字列から入力を取得するためのもので、もう 1 つは結果から入力とその意味を取得するためのものです。
Dim textToParse As String = "\\input var: x(length),y(height),z(width)"
' Regex matches the start of the string and zero or more input(meaning) portions
Dim extractInputsSectionRegex As New Regex("\\\\input\s*(?<variable>var):\s*(?<inputs>(\w+\(\w+\),*\s*)*)")
' Regex matches an individual input and meaning and returns the captures in named groups
Dim extractIndividualInputsRegex As New Regex("(?<input>\w+)\((?<meaning>\w+)\)")
' Match the input string to extract inputs and meanings
Dim initialMatch As Match = extractInputsSectionRegex.Match(textToParse)
If initialMatch.Success = True Then
' Extract inputs and meanings
Dim inputsSection As String = initialMatch.Groups("inputs").Value
' Match one or more input(meaning) portions
Dim inputMatches As MatchCollection = extractIndividualInputsRegex.Matches(inputsSection)
If inputMatches.Count > 0 Then
' Loop through each match found
For Each inputMatch As Match In inputMatches
' Extract input and meaning
Dim input As String = inputMatch.Groups("input").Value
Dim meaning As String = inputMatch.Groups("meaning").Value
' Display
Console.WriteLine("Input: " & input & ", meaning: " & meaning)
Next
End If
End If
指定された入力と次の文字列を使用してテストされました。
\\input var: vol(volume),a(area)
\\input var: x(length), y(height), z(width)