【问题标题】:Capture contents inside curly braces捕获大括号内的内容
【发布时间】:2017-04-19 18:15:13
【问题描述】:
因此,作为我的应用程序的一部分,我需要它从文本文件中读取数据,并获取大括号之间的元素。
例如:
Server_1 {
/directory1 /directory2
}
Server_2 {
/directory1
/directory2
}
然后类似,如果Server == Server_1,打印目录。
【问题讨论】:
标签:
python
regex
string
parsing
text
【解决方案1】:
你可以试试这个:
\{(.*?)\}
Explanation
\{ matches the character { literally (case sensitive)
(.*?) 1st Capturing Group
-
.*? 匹配任意字符
-
*? Quantifier — 匹配零次到无限次,尽可能少,根据需要扩展(惰性)
-
\} 匹配字符 } 字面意思(区分大小写)
提取大括号内内容的示例代码:
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
【解决方案2】:
如果你还想提取服务器名称,可以试试这个:
fullConfig = """
Server_1 {
/directory1 /directory2
}
Server_2 {
/directory1
/directory2
}
"""
# OUTPUT
# ('Server_1', '/directory1 /directory2\n')
# ('Server_2', '/directory1\n /directory2\n')
regex = r'(\w+)\s*[^{]*{\s*([^}]+)\s*}'
for serverName, serverConfig in re.findall(regex, fullConfig):
print(serverName, serverConfig)