Reputation: 37633
I have following pattern {(.*?)}
and it matchs 1 item only.
How I can match multiple items in C# from this text
akjsd{OrderNumber} aksjd {PatientName} aksjak sdj askdj {PatientSurname} askdjh askdj {PatientNumber} aksjd aksjd aksjd kajsd kasjd
to get list like
{OrderNumber}
{PatientName}
{PatientSurname}
{PatientNumber}
Thank you!
Upvotes: 2
Views: 1231
Reputation: 41838
Use this regex {[^}]*}
(more efficient because .*?
backtracks at each step) and do it like this:
var resultList = new StringCollection();
var myRegex = new Regex("{[^}]*}", RegexOptions.Multiline);
Match matchResult = myRegex.Match(yourString);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
Console.WriteLine(matchResult.Value);
matchResult = matchResult.NextMatch();
}
Upvotes: 3
Reputation: 7017
Something like this perhaps?
string input = "akjsd{OrderNumber} aksjd {PatientName} aksjak sdj askdj {PatientSurname} askdjh askdj {PatientNumber} aksjd aksjd aksjd kajsd kasjd";
MatchCollection matches = Regex.Matches(input, "{(.*?)}");
foreach(Match match in matches)
{
Console.WriteLine(match.Value);
}
Upvotes: 4