Reputation: 11
I have raw data in the format:
<&70><+><&10><+>.002<&70><+>B<&70><+>A<+><&90>
I would like to use VB to extract the contexts between <
and >
and store them in an array, such as {&70,+,&10,+, ... }
Then I would like to do the same for the items NOT between the brackets, also storing in an array like {.002, B, A}
Does anyone know how I can do this using regex?
Upvotes: 0
Views: 797
Reputation: 5049
Storing in an array could be difficult upfront because you don't know which size they will be ; but you can use List(Of String)
instead (and as final step call the ToArray extension method
on them if you really want)
Using the regex given by sln (why is way better than what I was doing) you need to loop through all the matches in your data using Regex.Matches (or it's instance counterpart if you want to create [and compile] the regex upfront)
And then if the current match has the indicated group (1 or 2 as shown in sln's regex explanation) add it to the corresponding list.
Dim rawData As String = "<&70><+><&10><+>.002<&70><+>B<&70><+>A<+><&90>"
Dim betweenBrackets As New List(Of String)
Dim outsideBrackets As New List(Of String)
For Each m As Match In Regex.Matches(rawData, "<([^>]*)>|((?:(?!<[^>]*>)[\S\s])+)")
If m.Groups(1).Success Then betweenBrackets.Add(m.Groups(1).Value)
If m.Groups(2).Success Then outsideBrackets.Add(m.Groups(2).Value)
Next
Upvotes: 1
Reputation:
I would do both operations at the same time in a global search.
If group 1 matched, push into bracket array,
if group 2 matched, push into non-bracket array.
# <([^>]*)>|((?:(?!<[^>]*>)[\S\s])+)
<
( [^>]* ) # (1)
>
|
( # (2 start)
(?:
(?! < [^>]* > )
[\S\s]
)+
) # (2 end)
Upvotes: 0