我需要有一个处理不同最终条件的正则表达式,我的想法是这样的,但它不起作用
re.findall(r'(?<=tests\[")(.*)((?="\])|(?=:)', input_string])
输入采用以下格式:
tests["Status code: " +responseCode.code] = responseCode.code === 200;
tests["Schema validator GetTerminalInitEventForHarewareIds"] = tv4.validate(data, schema);
答案 0 :(得分:1)
^tests\["(.*?)(?::\s*"\s*[^\]]|"\])
此正则表达式将执行以下操作:
tests["
现场演示
https://regex101.com/r/oE9lL6/3
示例文字
tests["Status code: " +responseCode.code] = responseCode.code === 200;
tests["Schema validator GetTerminalInitEventForHarewareIds"] = tv4.validate(data, schema);
样本匹配
: "
后跟非]
结尾,则不包含:
和尾随文字: "]
结尾,则包含:
和尾随空格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
----------------------------------------------------------------------