如何捕获重复的可选值

时间:2017-10-11 21:54:04

标签: regex

我所有尝试捕获之前的所有内容,“”只返回最后一次出现。我有以下两个源字符串示例。

{"rec_timestamp": "2016-12-25T11:43:38.763-0700","151521": "-18.788","151515": "25","151522": "2.804","151520": "3506","151523": "-18.4","11124": "NO_DEFECT|","151516": "0","151510": "ALCLFA2BA314","151518": "3.28","151519": "24.398438","": "","": "","": "","": ""}
{"rec_timestamp": "2016-12-25T11:43:38.763-0700","151521": "-18.788","151515": "25","151522": "2.804","151520": "3506","151523": "-18.4","11124": "NO_DEFECT|","151516": "0","151510": "ALCLFA2BA314","151518": "3.28","151519": "24.398438"}

因此,对于第一条记录,我最多......“24.398438”返回$ 1(之前的所有内容,“”),第二个示例按原样返回整行(''是可选的)?

我试过了:

(^.*)(,""*?)
(^.*)(,"".*)

1 个答案:

答案 0 :(得分:0)

使用您使用的语言,默认情况下正则表达式是贪婪的:.*部分希望尽可能长时间匹配。您可以通过将.*替换为.*?来无限制地使用正则表达式(它希望匹配尽可能少的字符)。

在这里,您的子字符串由开头的行的开头和行尾的字符,""或字符}分隔。通过这样的方式,您可以获得标准化匹配(省略}字符)

^(.+?)(,"".*|\}$)

如果您不需要捕获第二部分,则可以使用断言。

^(.+?)(?=,""|\}$)