Ger Cas
Ger Cas

Reputation: 2298

How to match every value within braces?

I have the following lines for which I want to match only the content within internal braces.

So for these 3 lines:

{ abv:{1} pwr:{1} dft:{1} valUy:{4066792} }
{ wwqe:{0x342} }
{ heew:{ValStr} abgd:{-} }

the output would be:

1,1,1,4066792
0x342
ValStr,-

I've tried with the following 2 regex testing here, but I'm not getting values I look for.

\{(.*?)\}
(?<=\{).+?(?=\})

Thanks in advance for any help.

Upvotes: 1

Views: 22

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521804

We can try using re.findall with the following regexp pattern:

\{([^{}]+)\}

This will capture the contents in between curly braces across the entire input. Note that it ensures that we do not attempt to match anything other than innermost braces.

inp = """{ abv:{1} pwr:{1} dft:{1} valUy:{4066792} }
{ wwqe:{0x342} }
{ heew:{ValStr} abgd:{-} }"""

matches = re.findall(r'\{([^{}]+)\}', inp)
print(matches)

This prints:

['1', '1', '1', '4066792', '0x342', 'ValStr', '-']

Upvotes: 3

Related Questions