【发布时间】:2019-06-03 04:09:59
【问题描述】:
我有来自regex capturing with repeating pattern的以下正则表达式
([0-9]{1,2}h)[ ]*([0-9]{1,2}min):[ ]*(.*(?:\n(?![0-9]{1,2}h).*)*)
它需要以下字符串
1h 30min: Title
- Description Line 1
1h 30min: Title
- Description Line 1
- Description Line 2
- Description Line 3
并因此产生这个
Match 1:
"1h 30min: Title
- Description Line 1"
Group 1: "1h"
Group 2: "30min"
Group 3: "Title
- Description Line 1"
Match 2:
"1h 30min: Title
- Description Line 1
- Description Line 2
- Description Line 3"
Group 1: "1h"
Group 2: "30min"
Group 3: "Title
- Description Line 1
- Description Line 2
- Description Line 3"
我现在有匹配的1h 30min 并不总是出现在新行上。所以说我有以下字符串
1h 30min: Title
- Description Line 1 1h 30min: Title - Description Line 1
- Description Line 2
- Description Line 3
如何修改正则表达式以获得以下匹配结果?
Match 1:
"1h 30min: Title
- Description Line 1"
Group 1: "1h"
Group 2: "30min"
Group 3: "Title
- Description Line 1"
Match 2:
"1h 30min: Title - Description Line 1
- Description Line 2
- Description Line 3"
Group 1: "1h"
Group 2: "30min"
Group 3: "Title - Description Line 1
- Description Line 2
- Description Line 3"
虽然删除 \n 可以解决问题,但它最终会匹配第一个 1h 30min 之后的所有内容
【问题讨论】:
标签: regex regex-lookarounds regex-group regex-greedy