So, as part of my application, i need it to read data from a text file, and get elements between curly brackets.
e.g:
Server_1 {
You can try with this:
\{(.*?)\}
Explanation
\{ matches the character { literally (case sensitive)(.*?) 1st Capturing Group.*? matches any character*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)\} matches the character } literally (case sensitive)Sample Code to extract content inside curly bracket:
import re
regex = r"\{(.*?)\}"
test_str = ("Server_1 {\n"
"/directory1 /directory2\n\n"
"}\n"
"Server_2 {\n\n"
"/directory1\n\n"
"/directory2\n\n"
"}")
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)
for matchNum, match in enumerate(matches):
for groupNum in range(0, len(match.groups())):
print (match.group(1))
Run the code here