vb.net在<>之间获取文本括号

时间:2016-05-10 08:32:09

标签: vb.net string brackets

我正在为Autodesk Inventor构建一个使用表达式字符串的工具。

=Pipe Ø<Pipe_OD> x <Pipe_t> - lg. <Pipe_length>mm

&lt; ...&gt;之间的文字几乎可以是用户输入的内容,因此这些值不固定。 &lt; ...&gt;的数量在字符串中可以从0到5不等。

我希望这个字符串的结果如下:

转换后的字符串,其中&lt; ...&gt;之间的值被数字替换为升值。

=Pipe Ø<1> x <2> - lg. <3>mm

string()存储由数字(上面)替换的值。

我找到了一种方法,可以在字符串中使用1 <...>的字符串,但现在数量是可变的,我对如何做到这一点一无所知。

Link to method

1 个答案:

答案 0 :(得分:1)

编辑:使用正则表达式的新答案(更好的是,仍会出现其他&lt;和&gt;但仍有问题)

    Dim s As String = "abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>k"
    Dim myValues As New List(Of String)

    Dim matches As MatchCollection = Regex.Matches(s, "<(.|\n)*?>", RegexOptions.IgnoreCase)

    Dim totalDiff As Integer = 0
    Dim idx As Integer = 0

    For Each ma As Match In matches
        Dim realIndex = ma.Index - totalDiff
        s = s.Remove(realIndex, ma.Length).Insert(realIndex, "<" & idx.ToString & ">")
        idx += 1
        totalDiff += ma.Length - (idx.ToString.Count + 2)
        myValues.Add(ma.Value.Trim({"<"c, ">"c}))
    Next

"abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>"将更改为"abc <0> 123 <1> &*( <2>"myValues将包含&#34; pipe_val1&#34;,&#34; pipe_val2&#34;和&#34; pipe_val3&#34;

相关问题