Dimi Dimi - 3 months ago 16
C# Question

How to match multiple items with Regex

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!

Answer

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);
}