I have a variable in the form of {varName} or {varName, “defaultValue”} and I want a regex that will match it. varName is only alphanumeric and “_” (\w+), and the default value can be anything except the combination of “} which signifies the end of the variable. White space doesn’t matter between the braces, comma, varName or defaultValue. So far the regex that I have come up with is
\{\s*(\w+)\s*(,\s*\"([^(\"\})]*)\"\s*)?\}
The problem is that the match ends at the first ” OR } and not the combination, i.e. {hello, “world”} does match but {hello, “wor”ld”} or {hello, “wor}ld”}
Any idea how to solve this? In case this helps, I’m coding it using Java.
I have found the solution to my own question, it’s only fair to share. The following regex:
Will do the trick, stopping at the first sequence of ‘”}’
In Java, this will be (continuing previous answer’s example):