正则表达式模式检查直到下一组模式但排除它

Regex pattern to check until next set of pattern but exclude it

我正在尝试匹配并保留以下所有文本(包括数字)直到下一组数字

string = "100 - Generic - Applicable (Prepaid leg) 101 - TFM - N/A 102 - TOPUP - Applicable (Prepaid leg) 103 - Staff - Applicable (Prepaid leg)"

我下面的正则表达式也抓取了下一组数字并打破了其余部分,如您所见

\b(\d{3}.*?\s\d{3})

基本上结果应该是这样的

list = ['100 - Generic - Applicable (Prepaid leg)','101 - TFM - N/A','etc','etc']

https://regex101.com/r/2QOMzy/1

我在最后一个模式中努力告诉 exclude\stop。 我看过这些,显然没有成功:

://whosebug.com/questions/33249035/regex-match-from-pattern-until-another-pattern

您可以省略捕获组并改用正先行。

\b\d{3}\b.*?(?=\s+\d{3}\b)

Regex demo

如果您还想包括最后一个匹配项,您可以添加一个替代来断言字符串的结尾。

\b\d{3}\b.*?(?=\s+\d{3}\b|$)

Regex demo