Regex 只能在满足条件的情况下捕获某个字符串吗?
Can Regex capture a certain string only if a condition is met?
我有这组数据:
- 结束日期":"2024-10-24","员工":"000A0001","提交":"N"
- 结束日期":"2023-10-18","员工":"000A0001","提交":"P"
- 结束日期":"2021-01-02","员工":"000A0001","提交":"J"[=31=]
- 结束日期":"2022-10-11","员工":"000A0001","提交":"M"
- 结束日期":"2020-09-18","员工":"000A0001","提交":"N"
我只想捕获提交值为 N 或 M 的 EndDate。
捕获的结束日期应该是:
- 2024-10-24
- 2022-10-11
- 2020-09-18
是的,您可以使用正则表达式前瞻来执行此操作:
\d{4}-\d{2}-\d{2}(?=.+"Submitted":"[NM]")
此处有更多详细信息:Regex lookahead, lookbehind and atomic groups
捕获日期并提前使用:
"EndDate":"([^"]+)"(?=.*"Submitted":"[NM]")
您的结束日期在比赛的第 1 组。
注意:我在正则表达式的开头添加了“””,假设您不小心从看起来是 json 的示例中省略了它。
我有这组数据:
- 结束日期":"2024-10-24","员工":"000A0001","提交":"N"
- 结束日期":"2023-10-18","员工":"000A0001","提交":"P"
- 结束日期":"2021-01-02","员工":"000A0001","提交":"J"[=31=]
- 结束日期":"2022-10-11","员工":"000A0001","提交":"M"
- 结束日期":"2020-09-18","员工":"000A0001","提交":"N"
我只想捕获提交值为 N 或 M 的 EndDate。
捕获的结束日期应该是:
- 2024-10-24
- 2022-10-11
- 2020-09-18
是的,您可以使用正则表达式前瞻来执行此操作:
\d{4}-\d{2}-\d{2}(?=.+"Submitted":"[NM]")
此处有更多详细信息:Regex lookahead, lookbehind and atomic groups
捕获日期并提前使用:
"EndDate":"([^"]+)"(?=.*"Submitted":"[NM]")
您的结束日期在比赛的第 1 组。
注意:我在正则表达式的开头添加了“””,假设您不小心从看起来是 json 的示例中省略了它。