vb.net 获取 <> 括号之间的文本
vb.net get text between <> brackets
我正在为 Autodesk Inventor 构建一个使用表达式字符串的工具。
=Pipe Ø<Pipe_OD> x <Pipe_t> - lg. <Pipe_length>mm
<...> 之间的文本几乎可以是用户输入的任何内容,因此这些值不是固定的。字符串中 <...> 的数量可以从 0 到 5 不等。
我希望此字符串的结果如下:
转换后的字符串,其中 <...> 之间的值替换为具有升序值的数字。
=Pipe Ø<1> x <2> - lg. <3>mm
还有一个string()
,其中存储了被数字(以上)替换的值。
我找到了一种适用于字符串中包含 1 <...>
的字符串的方法,但现在数量是可变的,我对如何执行此操作一无所知。
Link to method
编辑:使用正则表达式的新答案(好多了,但仍然会有额外的 < 和 > 问题)
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
将包含 "pipe_val1"、"pipe_val2" 和 "pipe_val3"
我正在为 Autodesk Inventor 构建一个使用表达式字符串的工具。
=Pipe Ø<Pipe_OD> x <Pipe_t> - lg. <Pipe_length>mm
<...> 之间的文本几乎可以是用户输入的任何内容,因此这些值不是固定的。字符串中 <...> 的数量可以从 0 到 5 不等。
我希望此字符串的结果如下:
转换后的字符串,其中 <...> 之间的值替换为具有升序值的数字。
=Pipe Ø<1> x <2> - lg. <3>mm
还有一个string()
,其中存储了被数字(以上)替换的值。
我找到了一种适用于字符串中包含 1 <...>
的字符串的方法,但现在数量是可变的,我对如何执行此操作一无所知。
Link to method
编辑:使用正则表达式的新答案(好多了,但仍然会有额外的 < 和 > 问题)
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
将包含 "pipe_val1"、"pipe_val2" 和 "pipe_val3"