Search code examples
regexrainmeter

Pulling multiple values from json with single regex


I'm trying to fetch multiple values in a single regex. I have a fiddle here: http://fiddle.re/tuaddd

I need to be able to take the following json and extract 4 values:

[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "4344.21", 
        "price_btc": "1.0", 
        "24h_volume_usd": "1503030000.0", 
        "market_cap_usd": "71799147986.0", 
        "available_supply": "16527550.0", 
        "total_supply": "16527550.0", 
        "percent_change_1h": "-0.29", 
        "percent_change_24h": "1.01", 
        "percent_change_7d": "5.32", 
        "last_updated": "1503839363"
    }
]

The values I need to extract are:

  • price_usd
  • percent_change_1h
  • percent_change_24h
  • percent_change_7d

Using price_usd": "(.+?)" works great for pulling a single value, but when I try to pull 2 using price_usd": "(.+?)"[.+?]percent_change_1h": "(.+?)" doesn't seem to pull any. How can I do this in the most efficient manner?

Update - I'm building a Rainmeter skin that shows cryptocurrencies. So I need perl regex that is capable of doing this as I'm somehwat limited in what I can do as it relates to their pre-existing plugins available. For example, here's what's there currently:

[MeasureBTC]
Measure=Plugin
Plugin=WebParser
URL=https://api.coinmarketcap.com/v1/ticker/bitcoin/
RegExp=price_usd": "(.+?)"

[MeasureBTCUSDValue]
Measure=Plugin
Plugin=WebParser
URL=[MeasureBTC]
StringIndex=1

As you can see, I essentially need a 1-liner to accomplish this as I'm not working with any programming language.


Solution

  • The solution to this issue is like so:

    RegExp=(?siU)price_usd": "(.*)".*percent_change_1h": "(.*)".*percent_change_24h": "(.*)".*percent_change_7d": "(.*)"