I have a file formatted like this:
hello = {
a = "2354a"
b = "06567567h"
}
goodbye = {
there = "/home/afhge"
}
...
anotherset = {
dsfsdf = grhbrwecs
dfgtmyj = 12345
}
I am using regular expressions in python, what I want matched is everything inside the braces so the resulting match output would be the following list:
['\n\n\ta = "2345a"\n\tb = "06567567h"\n\n\n', '\n\there = "/home/afhge"\n\n', '\n\tdsfsdf = grhbrwecs\n\tdfgtmyj = 12345\n\n']
I have tried the regex:
desired_output = re.findall("{[^}]", file_text)
however this regex results in the list:
['{\n', '{\n', '{\n', '{\n', '{\n']
It looks like [^}] matches any character up until a newline. I’ve tried doing:
desired_output = re.findall("{[^}]", file_text, re.S)
and
desired_output = re.findall("{[^}]", file_text, re.M)
To no success :(.
Thanks!
No your character class is right. It will match any character that is not
}(even line breaks). The problem is that[^}]matches only one character. Simply use a repetition quantifier (and you should probably escape the{):Regarding the options you tried. If anything
re.Swould help, because without it.does not match line breaks. But the.is really the only thing affected byre.S. The other optionre.Mhas nothing to with it. That just makes the anchors^and$match at line beginnings and ends as well.Also, since you only want the content within the brackets, you don’t need to match the
{itself, but you could use a lookbehind instead:This will not include the
{in the match.