Trying to get a substring out of a larger string from an Azure Subnet. The vnet resource id is identical to the subnet resource id minus the last 2 segments. User inputs the resource id of the subnet, and I want to get the resource id of the vnet out of it. The vnets could have different naming conventions, so the length of the "substring" won't always be the same, but the vnet id will always be the subnet id minus the last 2 segments.
For example, is there an easy way to get string2 from string 1, where the length of string 2 might vary. Let's say string1 is:
abc/def/ghi/jkl/mno
and I want to get the first 3 segments of this string so that string2 will be:
abc/def/ghi
Is there an easy operation that can be performed that will cut off the last 2 segments despite the length of, in this case, the first three? To clarify with different lengths, if the user then puts in a different string1:
abcdef/ghi/jklmn/opqr/stuvwx
string2 should be:
abcdef/ghi/jklmn
I have tried using .split but it only gives me the text after the delimiter and .substring I believe needs a start and end index, which again might vary.
'abcdef/ghi/jklmn/opqr/stuvwx' -replace '(?<=(?:.+/){2}.+)/.+'