这是一个解决方案,我使用了两个正则表达式,一个从原始字符串中获取输入,然后另一个从结果中获取您的输入及其含义。
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)