regex - 仅当先前的组匹配将捕获设置为空时才捕获条件正则表达式?
问题描述
试图让条件正则表达式工作以使用https://regex101.com/r/51mp2i/1中的 2 个事件捕获域和用户代理值, 但仅获得 1 个匹配项。如何使用条件正则表达式更新正则表达式以获取 2 个匹配项?谢谢。
Match 1:
domain: example.org
useragent: "" or not capture
Match 2:
domain: example.org
useragent: Mozilla/5.0 (compatible;example-checks/1.0;+https://www.example.com/; check-id: 9EXc112795a4766a)
解决方案
采用
"headers":\s+\[{"name":\s+"Host",\s+"value":\s+"(?<domain>[^"]+)(?:.*?"(?i)User-?(?i)Agent",\s+"value":\s+"(?<useragent>[^"]*))?
见证明。
解释
--------------------------------------------------------------------------------
"headers": '"headers":'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------------------
{"name": '{"name":'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
"Host", '"Host",'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
"value": '"value":'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^"]+ any character except: '"' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
"User '"User'
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
Agent", 'Agent",'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
"value": '"value":'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
)? end of grouping
推荐阅读
- javascript - REPL 中 NodeJs 预加载选项 (-r) 和显式要求之间的区别
- reactjs - 错误:无法解析模块 ./Easing from node_modules\react-native-reanimated\src\Animated.js
- javascript - 需要并排显示页眉和图像
- angular - 将 mt HTTP 转换为 Observable 时出现多个错误
- scala - 功能存储库 - 基于 find()/create() 方法构建 get()
- python - 如何使用 python-docx 向 Word 文档添加超链接?
- python - 如果字符串在列表中,如何用它的字母替换 \u0000
- javascript - 随机播放数组在 Javascript 中给出相同的值
- bert-language-model - 无法执行 BioBERT run_factoid.py 程序
- gcc - 长“来回”gcc库路径背后的原因