0

I have s string like this

"Country": "FRANCE", "PostalCode": "01, 02, 03"

When I use this regex:

"(.*)": "(.*)"

The system return me only one match with 2 groups

MATCH 1
1.  [1-32]  `Country": "FRANCE", "PostalCode`
2.  [36-70] `01, 02, 03`

What I do wrong ? I would like to get any match value and I can have many "xxx": "yyy" token separated by a comma. So my string can also be

"Country": "FRANCE", "PostalCode": "01, 02, 03", "Xxxxx": "yy", "Aaaaa5": "b", "Mmmmmmm": "n1, n2, n3-n30"
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • Looking for [`"([^"\\]*(?:\\.[^\\"]*)*)"\s*:\s*"([^"\\]*(?:\\.[^\\"]*)*)"`](https://regex101.com/r/qE0zB3/1)? – Wiktor Stribiżew Nov 17 '15 at 10:12
  • Which language are you using? – Mayur Koshti Nov 17 '15 at 10:13
  • I don't want to link this question to a specific language. – Bastien Vandamme Nov 17 '15 at 10:19
  • If you know there's no escaped quotes in between quotes and need more performance, use negation mentioned by @MayurKoshti `"([^"]*)"\s*:\s*"([^"]*)"` [like this](https://regex101.com/r/nT8uC0/1). Your regex failed [because of greed](http://www.regular-expressions.info/repeat.html#greedy) which is addressed by @CladClad answer. – bobble bubble Nov 17 '15 at 10:40

2 Answers2

1

Use this : "(.*?)": "(.*?)" instead as you want the smaller group as possible.

enter image description here

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
  • I would not use that. It is (almost) the same regex I posted in my comment, but in a lazy dot matching style which is not efficient. Also, it will fail with `"Model": "Some \"Model name\" here"`. – Wiktor Stribiżew Nov 17 '15 at 10:15
  • 1
    My solution is just an easy way considering what he will have inside his datas of course it ain't the only solution but it works in most cases ;) – Clad Clad Nov 17 '15 at 10:20
  • 1
    Yes, it is a good solution for students, not for use in production. – Wiktor Stribiżew Nov 17 '15 at 10:21
  • *? Between zero and unlimited times, as few times as possible, expanding as needed. I didn't knew the combination of * and ? is actually another quantifier that is not just the sum of * and ?. – Bastien Vandamme Nov 17 '15 at 10:24
  • @stribizhev How do you know this one is not efficient? What should I avoid? – Bastien Vandamme Nov 17 '15 at 10:27
  • @B413: This `*?` is called a *lazy quantifier*, and this one matches 0 or more characters, but as few as necessary to find a valid match. See [*Mastering Quantifiers* at rexegg.com](http://www.rexegg.com/regex-quantifiers.html#lazy_solution). This is a poorman's solution that should be replaced with [*unrolling the loop*](http://stackoverflow.com/a/17043605/3832970) technique if you plan to use it in production. *How do you know this one is not efficient?* - Compare [this](https://regex101.com/r/qE0zB3/1) (36 steps) and [this](https://regex101.com/r/qE0zB3/2) (92 steps). – Wiktor Stribiżew Nov 17 '15 at 10:29
  • Thank you for this valuable information – Bastien Vandamme Nov 17 '15 at 10:32
1
$s = '"Country": "FRANCE", "PostalCode": "01, 02, 03"';
REGEX = '/\"([^\"]+)\"\s*\:\s*\"([^\"]+)\"/';
preg_match_all(REGEX, $s, $res);
print_r($res);
Mayur Koshti
  • 1,794
  • 15
  • 20