Regex value between brackets

P

Peter Proost

Hi group I'm searching for a regex to retrieve the values from between
the brackets.

For example if I've got this text:

({CP-2} x {EDP-2}) / 1000 = {KGP-2} = f

Prix de base = f + VA (fixe jusqu’au {TWD})
= {KGP-2} + {TW} = {(KGP-2) + TW}

I would like to get

{CP-2}
{EDP-2}
{KGP-2}
{TWD}
{KGP-2}
{TW}
{(KGP-2) + TW}

I thought I could do it with this regex: new Regex("{.*}") but it
doesn't work as I expected.

All tips are welcome

Greetz,

Peter
 
M

Martin Honnen

Peter said:
I thought I could do it with this regex: new Regex("{.*}") but it
doesn't work as I expected.

..* matches greedily so you need .*? at least to ensure you match only to
the first following curly brace:

string s = "= {KGP-2} + {TW} = {(KGP-2) + TW}";
foreach (Match m in Regex.Matches(s, "{.*?}"))
{
Console.WriteLine(m.Value);
}
 
P

Peter Proost

.* matches greedily so you need .*? at least to ensure you match only to
the first following curly brace:

             string s = "= {KGP-2} + {TW} = {(KGP-2) +TW}";
             foreach (Match m in Regex.Matches(s, "{.*?}"))
             {
                 Console.WriteLine(m.Value);
             }

Thanks, that was exactly what I needed,

thanks again.

Greetz,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top