正则表达式需要有两个条件结束条件

时间:2016-06-08 16:28:27

标签: python regex

我需要有一个处理不同最终条件的正则表达式,我的想法是这样的,但它不起作用

re.findall(r'(?<=tests\[")(.*)((?="\])|(?=:)', input_string])

输入采用以下格式:

tests["Status code: " +responseCode.code] = responseCode.code === 200;
tests["Schema validator GetTerminalInitEventForHarewareIds"] = tv4.validate(data, schema);

1 个答案:

答案 0 :(得分:1)

描述

^tests\["(.*?)(?::\s*"\s*[^\]]|"\])

Regular expression visualization

此正则表达式将执行以下操作:

  • 要求字符串以tests["
  • 开头
  • 匹配第一个双引号内的子字符串

实施例

现场演示

https://regex101.com/r/oE9lL6/3

示例文字

tests["Status code: " +responseCode.code] = responseCode.code === 200;
tests["Schema validator GetTerminalInitEventForHarewareIds"] = tv4.validate(data, schema);

样本匹配

  • 捕获组0获得整场比赛
  • 捕获组1只获取第一个引号内的值。
    • 如果值以: "后跟非]结尾,则不包含:和尾随文字
    • 如果值以: "]结尾,则包含:和尾随空格
MATCH 1
1.  [7-18]  `Status code`

MATCH 2
1.  [78-129]    `Schema validator GetTerminalInitEventForHarewareIds`

MATCH 3
1.  [169-224]   `Schema validator GetTerminalInitEventForHarewareIds: `

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  tests                    'tests'
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    :                        ':'
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [^\]]                    any character except: '\]'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
    \]                       ']'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------