-1

First of all, this doesn't answer my question. I've tried almost all of those and they mostly match within explicitly specified 1 extra new line.

The question: say i have dictionary, or any other JSON-like object. I have many of them in the file and i want to match every of them. For example:

some_var = {
    'foo': 'bar',
    'list_foo': ['bar1', 'bar2'],
}
other_var = [
    {
        'foo1': 'bar1',
        'foo2': True
    },
    {
        'foo_list1': ['bar3', 'bar4'],
        'foo2': 'bar2'
    },
]

Values may be anything, but dict. So no nested dicts.

My ultimate goal is to match everything that lays between each set of {} including the figure brackets (optional, but desirable). So that when i run the regex, i will get these matched:

{
    'foo': 'bar',
    'list_foo': ['bar1', 'bar2'],
}
{
    'foo1': 'bar1',
    'foo2': True
}
{
    'foo_list1': ['bar3', 'bar4'],
    'foo2': 'bar2'
}

One of my closes attempt is:

\{(.*\n)*\}

But it matches everything between the very first { and very last } in a file. There was many other attempts, but none worked for variable dict size.

I'm running this regex in PyCharm in an attempt to quickly refactor some code.

Please, do not provide workarounds. I have already solved the problem in a different way, but am still curious about this exact solution.

go2nirvana
  • 1,299
  • 7
  • 16

1 Answers1

0

Use {[^}]+}

the regex should include anything except a closing curly brace.

Demo: https://regex101.com/r/d6NdrJ/1

mankowitz
  • 1,383
  • 1
  • 9
  • 23