0

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.

Ben
  • 60,438
  • 111
  • 314
  • 488
  • what's your OS? – RomanPerekhrest Aug 27 '17 at 13:30
  • 1
    Use a JSON parser, or else this happens https://stackoverflow.com/a/1732454/5087125 – pvg Aug 27 '17 at 13:31
  • 1
    You _absolutely_ should be using a JSON parser here if at all possible. Update your question with the tool/language you are using, and if it doesn't support JSON parsing then hopefully you would be able to switch to something which does. – Tim Biegeleisen Aug 27 '17 at 13:31
  • 1
    you should never parse JSON with regex – RomanPerekhrest Aug 27 '17 at 13:33
  • 1
    The reason why this doesn't work is that `.` doesn't match a newline. As we all strive to be good software engineers I'd suggest to refrain from parsing JSON with a regex. – yacc Aug 27 '17 at 13:37

1 Answers1

0

The solution to this issue is like so:

RegExp=(?siU)price_usd": "(.*)".*percent_change_1h": "(.*)".*percent_change_24h": "(.*)".*percent_change_7d": "(.*)"
Ben
  • 60,438
  • 111
  • 314
  • 488
  • You are hardcoding a space after colons. That is just the start of problems. Please read the answer linked by @pvg's comment on the question; we don't want an unholy child weeping the blood of virgins, do we? – Ed. Aug 27 '17 at 17:56
  • 1
    @Ed. If you understood skinning for Rainmeter, you'd understand the need for this kind of simple solution. – Ben Aug 27 '17 at 20:05