这条线的模式是什么
\\input var: x(length),y(height),z(width)
我想知道变量的命名组(比如这个x,y,z)和特定变量的命名组(就像这个长度,宽度,高度一样)
我尝试了这个文本行的模式
\\input var: x(length),y(height),z(width)
[\/\/\binput\b\s?\bvar\b:\s](\w+)\((.*?)\)\,?
我只能将group(1).value作为x group(2).value as length
我将如何找到其他值,如y,height,z,width
答案 0 :(得分:2)
这是一个解决方案,我使用了两个正则表达式,一个用于从原始字符串中获取输入,然后是另一个用于获取输入及其含义的结果。
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)