-3

I have a json object with value in this pattern: { customer:1501 } bought a car { car:6333 } How to use regexp to extract the digit of customer and car. I am very new to regex, I can only extract the string between the curly braces. I am not sure should I do it separately, i.e. extract the string between the curly braces than extract the digits after "customer:" or "car:". Please help

MIT
  • 109
  • 1
  • 7

1 Answers1

1

You can use this regex to match the curly braces and match customer or car and then the digits, and capture your digits from group1,

{\s*(?:customer|car):(\d+)\s*}

Explanation of regex:

  • { - Match a literal {
  • \s* - Match optional whitespace
  • (?:customer|car): - Match either customer or car literally followed by a colon
  • (\d+) - Matched one or more digits and capture it in group1
  • \s*} - Match optional space then closing curly bracket }

Demo

Let me know if you have any issues further.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36