0

Find string between two strings. I am getting only first group but there are more strings that matches regex but I don't know what I am missing. Below is my regex.

(?<="product": ")(.*?)(?=")

Input: {"product": "product1"},{"product": "product2"},{"product": "product3"}

Output: product1, product2, product3

Output will be in groups but yeah final output will be list of products.

Also see this, https://regex101.com/r/kGdgjY/1/

displayname
  • 317
  • 1
  • 5
  • 15
  • 1
    It's a part of a JSON string. Parse the JSON if you have an entire JSON string, otherwise add square brackets to transform it into a JSON array. Whatever, don't use regex to do that. – Casimir et Hippolyte Jun 20 '18 at 20:43
  • The problem is that is a part of string. And there is no other way to parse it other than regex – displayname Jun 20 '18 at 20:47
  • 1
    Just use the `/g` (for **g**lobal) flag, but I agree - regexing JSON is a hack, I would find a way to parse the JSON. – i-- Jun 20 '18 at 20:50
  • @displayname: In this case show the entire string. – Casimir et Hippolyte Jun 20 '18 at 20:52
  • @i-- It can be dupe of both, just close the question and we'll add both as close reasons (it is possible to add up to 5 close reasons). – Wiktor Stribiżew Jun 20 '18 at 21:03
  • Most of the time, this kind of questions are about a JSON string with an header that is easy to remove. That's why without to know what is the exact context, I don't close the question. – Casimir et Hippolyte Jun 20 '18 at 21:06
  • All of them gives output for 1st group matched only. – displayname Jun 20 '18 at 21:08
  • Thanks guys for the revert. I have updated question. I hope this will provide more info. – displayname Jun 20 '18 at 21:11
  • Is it the entire string or a part already extracted from another string? – Casimir et Hippolyte Jun 20 '18 at 21:12
  • @CasimiretHippolyte Input could be something like this Input `This are the list of products {"product": "product1"},{"product": "product2"},{"product": "product3"} and list of regions {"region": "region1"},{"region": "region2"},{"region": region3"}` So highlighted one is the whole string it can contain any random data as its a log of a application – displayname Jun 20 '18 at 21:13
  • 1
    What's wrong with `"product":\s*"([^"]+)"`? Live demo https://regex101.com/r/Ccivny/1 – revo Jun 20 '18 at 21:13
  • @revo Yeah that would do the job. Thanks. I hope it was not duplication. – displayname Jun 20 '18 at 21:18
  • @displayname: show the real string, not something you think it describes your real string. I think you have missing something easy to split the part that is JSON from the other part of the string. – Casimir et Hippolyte Jun 20 '18 at 21:26
  • @CasimiretHippolyte Check my previouse message where i tagged you. Highlighted will be the actual input, `This are the list of products {"product": "product1"},{"product": "product2"},{"product": "product3"} and list of regions {"region": "region1"},{"region": "region2"},{"region": region3"}` – displayname Jun 21 '18 at 13:19

0 Answers0