0

我有这个字符串:

{"TimePeriod": {"Start": "2017-03-01", "End": "2017-04-01"}, "Total": {"UnblendedCost": {"Amount": "2942.25119998", "Unit": "USD"}, "UsageQuantity": {"Amount": "20835", "Unit": "Hrs"}}, "Groups": [], "Estimated": false},
{"TimePeriod": {"Start": "2017-04-01", "End": "2017-05-01"}, "Total": {"UnblendedCost": {"Amount": "2982.62609983", "Unit": "USD"}, "UsageQuantity": {"Amount": "21049", "Unit": "Hrs"}}, "Groups": [], "Estimated": false},
{"TimePeriod": {"Start": "2017-05-01", "End": "2017-06-01"}, "Total": {"UnblendedCost": {"Amount": "1399.04829988", "Unit": "USD"}, "UsageQuantity": {"Amount": "23010", "Unit": "Hrs"}}, "Groups": [], "Estimated": false},
{"TimePeriod": {"Start": "2017-06-01", "End": "2017-07-01"}, "Total": {"UnblendedCost": {"Amount": "962.47549987", "Unit": "USD"}, "UsageQuantity": {"Amount": "20049", "Unit": "Hrs"}}, "Groups": [], "Estimated": false}

我正在使用正则表达式将上述字符串拆分为多条记录,例如:每条记录如下所示:

{"TimePeriod": {"Start": "2017-06-01", "End": "2017-07-01"}, "Total": {"UnblendedCost": {"Amount": "962.47549987", "Unit": "USD"}, "UsageQuantity": {"Amount": "20049", "Unit": "Hrs"}}, "Groups": [], "Estimated": false}

我目前的做法是

(\{\"TimePeriod\":){1}.+(false\}){1}

但这将匹配整个字符串而不是匹配每条记录,我认为解决方案应该是正则表达式中的前瞻,以确保TimePeriod在匹配的字符串中只出现一次,但我不知道该怎么做。任何指针将不胜感激。

*每行之间没有换行符,我只是把它放在那里进行演示

4

2 回答 2

1

这似乎可以满足您的需要。我只是稍微将您的正则表达式更改为搜索lazy模式.+?greed .+

(\{\"TimePeriod\":){1}.+?(false\}){1}

演示

如果添加更多修改,它将是

(\{\"TimePeriod\":).+?(false\})

另一种使用前瞻的方法,

(\{\"TimePeriod\":)(?:(?!false).)+(false\})
于 2018-04-06T03:10:51.947 回答
0

您可以拆分以下环视:

,(?=\{"TimePeriod":)

演示

逻辑基本上是在文本后面紧跟逗号的地方进行拆分{"TimePeriod":。请注意,这意味着文本的开头不会有拆分,因为没有逗号。

于 2018-04-06T01:38:07.390 回答