Regex Non-greedy Trick Nov 25th 2021 Words: 80

Regex matches the pattern greedily by default, for example:

1
{"a":"str bar","b":123,"c":"xyz"}

The pattern "str.+" matches the whole "str","b":123,"c":"xyz"

A quick fix is to replace . with [^"], so the pattern looks like "str[^"]+".

It matches anything but the double quote, thus appears to be “non-greedy”.