Stephen Stephen - 1 month ago 11 Question

Find all instances of a string enclosed by 2 specific characters at the start and end

I have searched all over and simply cannot get this one figured out. I've been trying to find all matches in a multi-line string such as:

Dim VariableText As String = "Some text <*

Dim Var1 as Integer = 5

ResponseText = IIf(Var1 <= 2,Var1 * 5, iif(Var1 > 2, Var1 * 2, 0)).ToString(""0"")

*> Some more text <* ResponseText = MsgBox(""TEST!"") *>"

Notice that there can be less-than, greater-than and asterisks within the enclosuers as well, so excluding a list of those characters does not work.

I have tried many different regex combinations, but I only seem to be able to capture everything between the very first "<*" and the very last "*>"

What I would like is to capture each of the sections enclosed by "<**>" so I can work with only the text inside the enclosure. For instance (and this is the closest regex expression I could find):

Dim R as New Regex("(?<=<\*).*(?=\*>)", RegexOptions.SingleLine)
Dim Coll as MatchCollection = R.Matches(VariableText)
For Each M as Match in Coll
'Do something with each section

I also tried an exclusion block as well: "(?<=<\*)?.(?:<\*)*(?=\*>)"
But that didn't work either

Does anyone know if this is possible with Regex or do I need to parse the string within a loop?


Please try the following. * is greedy and it will try to match the longest string, by using *? it will try to match the shortest.