如何提取regex查询直到一个特定的单词?

2024-03-28 11:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从LookML(一种特定的标记语言)中提取某些数据。如果这是示例代码:

explore: explore_name {}
explore: explore_name1 {
  label: "name"
  join: view_name {
      relationship: many_to_one
      type: inner
      sql_on: ${activity_type.activity_name}=${activity_type.activity_name} ;;
  }
}
explore: explore_name3 {}

然后我会收到一个列表,看起来像:

  • explore: character_balance {}
  • label: "name"
    join: activity_type {
      relationship: many_to_one
      type: inner
      sql_on: ${activity_type.activity_name}=${activity_type.activity_name} ;;
    }```
    
  • explore: explore_name4 {}

基本上,我在“explore”开始一场比赛,当我找到另一个“explore”时结束比赛,然后开始下一场比赛。你知道吗

这是我之前所做的,它匹配所有的行,直到找到一个;,这非常好:'explore:\s[^;]*'。但是,假设有一个“;”的话,它就停止了。你知道吗

我该如何改变这一点,以便它去掉“explore”和“explore”之间的所有内容?只要用explore替换regex中的“;”,只要它找到一个与[e,x,p,l,o,r,e]中的任何内容匹配的字母,就会停止,这不是我想要的行为。删除方括号和^会破坏所有内容,因此无法跨多行查询。你知道吗

我该怎么办?你知道吗


Tags: toname内容sqlontypeactivityone
3条回答

虽然它在Regex中是可以做到的,但是您应该使用一个能够理解格式的解析器,因为Regex解决方案非常脆弱。你知道吗

说到这里,这里有一个启用了DOTALL模式(其中.匹配任何字符,包括换行符)的正则表达式解决方案:

re.findall(r'explore:.*?\}', text, re.DOTALL)
  • explore:逐字匹配
  • .*?\}非贪婪匹配到下一个}

示例:

In [1253]: text = '''explore: character_balance {} 
      ...: explore: tower_ends { 
      ...:   label: "Tower Results" 
      ...:   join: activity_type { 
      ...:       relationship: many_to_one 
      ...:       type: inner 
      ...:       sql_on: ${activity_type.activity_name}=${wba_fact_activity.activity_name} ;; 
      ...:   } 
      ...: } 
      ...: explore: seven11_core_session_start {}'''                                                                                                                                                        

In [1254]: re.findall(r'explore:.*?\}', text, re.DOTALL)                                                                                                                                     
Out[1254]: 
['explore: character_balance {}',
 'explore: tower_ends {\n  label: "Tower Results"\n  join: activity_type {\n      relationship: many_to_one\n      type: inner\n      sql_on: ${activity_type.activity_name}',
 'explore: seven11_core_session_start {}']

您可以使用带有lookahead断言的非贪婪匹配来检查是否存在另一个explore:或字符串的结尾。尝试:

^{}

一个简单的方法是找到下一个“探索”字。但是如果由于任何原因,字符串值包含这个词,您将得到错误的结果。当字符串包含嵌套的方括号时,如果尝试停止使用花括号,也会出现同样的问题。你知道吗

这就是为什么我建议对字符串的语法进行更精确的描述,考虑字符串和嵌套的花括号。由于re模块没有递归功能(用于处理嵌套结构),因此我将使用pypi/regex模块:

import regex

pat = r'''(?xms)
    \b explore:
    [^\S\r\n]* # optional horizontal whitespaces
    [^\n{]* # possible content of the same line
    # followed by two possibilities
    (?: # the content stops at the end of the line with a ;
        ; [^\S\r\n]* $
      | # or it contains curly brackets and spreads over eventually multiple lines
        ( # group 1
            {
                [^{}"]*+ # all that isn't curly brackets nor double quotes
                (?:
                    " [^\\"]*+ (?: \\. [^\\"]* )*+ " # contents between quotes
                    [^{}"]*

                  |
                    (?1) # nested curly brackets, recursion in the group 1
                    [^{}"]*
                )*+
            }
        )
    )'''

results = [x.group(0) for x in regex.finditer(pat, yourstring)]

demo

更严格地说,您可以添加对单引号字符串的支持,还可以防止模式开头的“explore:”不在使用(*SKIP)(*FAIL)构造的字符串中。你知道吗

相关问题 更多 >