Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regex will capture multiple instances inside braces/parentheses?

Tags:

python

regex

How do I use regex to capture, say, every run of space characters \ + inside brackets? For example, in the string, "abc and 123 {foo-bar bar baz } bit {yummi tummie} byte." I should find four matches inside {} -- but nothing else. Assume Python language and that the string content is unknown.

EDIT: Also assume that there are NO nested braces.

like image 525
Eyeofpie Avatar asked Mar 13 '23 03:03

Eyeofpie


1 Answers

A lookahead can check if there is a } ahead without any { in between.

\s+(?=[^{]*})
  • \s is a short for whitespace character [ \t\r\n\f]. Match + one or more.

  • (?=[^{]*}) looks ahead if there is a } with any non { in between.

Demo at regex101

like image 74
bobble bubble Avatar answered Mar 15 '23 15:03

bobble bubble