Mech_Engineer Mech_Engineer - 1 year ago 90 Question get text between <> brackets

I am building a tool for Autodesk Inventor that works with an expression string.

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

The text between <...> can be almost anything it's what the user made as input so these values are not fixed. And the number of <...> in the string can vary from 0 to 5.

What I would like to have as result for this string is following:

A converted string where the values between the <...> are replaced by number with an ascending value.

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

And a
where the values that are replaced by the numbers (above) are stored in.

I found a method that can work for strings with 1
in the string but now that the quantity is variable I'm clueless about how I can do this.

Link to method

Answer Source

Edit: New answer with regular expressions (much better, will still have problems with additional < and > though)

    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}))

"abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>" will be changed to "abc <0> 123 <1> &*( <2>" and myValues will contain "pipe_val1", "pipe_val2" and "pipe_val3"