非贪婪数字范围的正则表达式

Regex for non-greedy range of digits

我正在尝试使用正则表达式抓取 JSON 数据中第一次出现的 InvoiceItemID,如下所示: 这是我目前拥有的正则表达式字符串: \d{6}(?=","Location":"ARN:190801210003100) 当前正则表达式字符串 returns 两个匹配项,但我只对第一个 occurrence/match 感兴趣。我知道我需要做的是使正则表达式成为非贪婪的,这通常涉及使用这样的东西:(.*?) 但我不知道在哪里实现这个非贪婪的代码。任何帮助,将不胜感激。谢谢。

如果需要进行测试,这里有一些原始数据:

{"ClientRef":"","Date":"2015-09-29 10:02:51 AM","InvoiceID":"451393","InvoiceItemID":"495340","Location":"ARN:193602013349538<br\/>16 LEIGHLAND DR , CITY OF MARKHAM, ON, L3R 7R4","ReportID":"268172,","Type":"ICI Commercial \/ Industrial Report"},{"ClientRef":"","Date":"2015-09-28 8:39:41 PM","InvoiceID":"451035","InvoiceItemID":"494939","Location":"ARN:190801210003100<br\/>2250 SHEPPARD AVE W, CITY OF TORONTO, ON, M9M 1L7","ReportID":"267810,","Type":"Basic Report"},{"ClientRef":"","Date":"2015-09-28 8:39:20 PM","InvoiceID":"451034","InvoiceItemID":"494938","Location":"ARN:190801210003100<br\/>2250 SHEPPARD AVE W, CITY OF TORONTO, ON, M9M 1L7","ReportID":"267809,","Type":"ICI Commercial \/ Industrial Report"},{"ClientRef":"","Date":"2015-09-28 2:59:03 PM","InvoiceID":"450515","InvoiceItemID":"494348","Location":"ARN:240201011110900<br\/>26-34 PLAINS RD E, BURLINGTON CITY, ON, L7T 2B9","ReportID":"267272,","Type":"ICI Commercial \/ Industrial Report"}

您的正则表达式不合适。您可以看到如何使用此正则表达式无法匹配所有 here. Here is a demo

InvoiceItemID":"(\d{6})