Reputation: 457
{{"name":"alpha",
"age":"23",
"sex":male",
"location":"U.S"
}
{"name":"beta",
"age":"23",
"sex":male",
"location":"Cambodia"
}}
If I give a name, my regex should return the location for that name. Suppose the name is given as a variable, then I try,
"name":"alpha"[\d\D]*"location":"(.+?)"
I always get the last location with this expression. How can I get the location based on the name? Any help?
Upvotes: 2
Views: 83
Reputation: 132879
You always get the last one because [\d\D]*
is matching everything in between and Regexes try to always match as much as possible. If your Regex engine knows about non-greedy matches (and it looks like it does because you use a non-greedy match at the location yourself: +?
), you should be able to use this:
"name":"alpha"[\d\D]*?"location":"(.+?)"
Upvotes: 2