Evgeniy
Evgeniy

Reputation: 2595

Match number value from JSON-LD with regex

I'm trying to get a number value from JSON-LD with regex. If i match a quoted value, like "price": "0.00" my regex works: "price": "(.*?)". But if the value is just a number, like "ratingValue": 5, my regex stops working.

What should be the regex to match the number in examples liek "ratingValue": 5? I tried "ratingValue": (.*?), but it doesn't work too.

Upvotes: 2

Views: 115

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

You can use

"(?:ratingValue|price)": *"?([0-9]+(?:[.][0-9]+)?)

See the regex demo. Details:

  • " - a double quote
  • (?:ratingValue|price) - either of the two alternative strings
  • ": - a ": string
  • *"? - zero or more spaces followed with an optional " char
  • ([0-9]+(?:[.][0-9]+)?) - Capturing group 1: one or more digits followed with an optional sequence of a dot and one or more digits.

Upvotes: 2

Related Questions