I’m trying to use RegEx to split a string into several objects. Each record is separated by a :, and each field is separated by a ~.
So sample data would look like:
:1~Name1:2~Name2:3~Name3
The RegEx I have so far is
:(?<id>\d+)~(?<name>.+)
This however will only match the first record, when really I would expect 3. How do I get the RegEx to return all matches rather than just the first?
Your last
.+is greedy, so it gobbles up theName1as well as the rest of the string.Try
This means that the Name can’t have a
:in it (which is probably OK for your data), and makes sure the name doesn’t grab into the next field.(And also use the Regex.Matches method which grabs all matches, not just the first).