【问题标题】: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

    1. \{ matches the character { literally (case sensitive)
    2. (.*?) 1st Capturing Group
    3. .*? 匹配任意字符
    4. *? Quantifier — 匹配零次到无限次,尽可能少,根据需要扩展(惰性)
    5. \} 匹配字符 } 字面意思(区分大小写)

    提取大括号内内容的示例代码:

     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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-02
        • 1970-01-01
        • 2017-06-01
        相关资源
        最近更新 更多