I have the string aa{{{a {{ {aaa{ that I would like to translate to aa { { {a { { {aaa {. Basically every { must a space character before it.
My regular expression substitution function I am currently using is: re.sub(r'[^\ ]{', lambda x:x.group(0)[0]+' {', test_case)
The result from the function is: aa {{ {a { { {aaa { (Close, but there is a {{ in the string)
My method performs very well on section like a{a{a. However if two { characters are together like a{{a it only seems to operate on the first { and completely neglect the following {.
A more clear example will be a large series of {{{{{{{{{{{{. My regex substitution returns:{ {{ {{ {{ {{ {{ {. Which clearly skips over every other character given tightly nested {.
Why are they skipping? Any help to untangle this confusion would be greatly appreciated!
P.S. I am sorry to everyone out there that have the strong desire to close all the opened curly-brace.
I’d use a negative lookbehind:
Basically we parse the string until we hit a
{. If the character before it isn’t whitespace (that’s the(?<!\s)bit), the{matches and we replace it with a space in front.