如何匹配"as much as possible until the group is matched"?

How to match "as much as possible until the group is matched"?

考虑字符串

hello world something 12345 somethingelse

我知道我将有 hello,然后是数字以外的一些字符(如果有帮助,我可以列出它们),然后是一组数字。我想匹配这组数字

如果我知道 hello 和数字之间的内容,我会使用 .*hello\sworld\s(\d*)\s 作为匹配字符串。

有没有办法说"match the group of digits after hello (that hello is followed only by non-digits characters up until the sought group)"(介于贪婪和惰性匹配之间的东西)

您可以使用这个正则表达式:

hello\D+(\d+)

其中:

  • \D+匹配hello
  • 后匹配1+个非数字
  • (\d+) 匹配 1+ 个数字并将其捕获到组 #1