0

这条线的模式是什么

\\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 作为长度

我将如何找到其他值,例如 y 、高度、z、宽度

4

1 回答 1

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)
于 2013-10-16T09:24:48.263 回答