Search code examples
regexsumologic

Split string with lookahead regex


I have this string:

{"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}

I am working on a regex to split the above string into multiple records, e.g: each record will look like:

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

My current approach is

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

But this will match the entire string instead of matching each record, I think the solution should be something with the lookahead in regex to ensure the TimePeriod appears just once in the matched string but I don't know how to do it. Any pointers will be appreciated.

*There is no newline between each line, I just put it there for the presentation


Solution

  • This seems work for your need. I just only slightly changed your regex to lazy searching mode.+? from greed .+

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

    Demo

    And if some more modification is added, it would be

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

    Another method using lookahead,

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