0

Using this Google App Script to get cryptocurrency prices from coinmarketcap: ( credit for this script goes to Josh Bradley via this stackoverflow topic: To exceed the ImportXML limit on Google Spreadsheet )

function importRegex(url, regexInput) {
  var output = '';
  var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  if (fetchedUrl) {
    var html = fetchedUrl.getContentText();
    if (html.length && regexInput.length) {
      output = html.match(new RegExp(regexInput, 'i'))[1];
    }
  }
  // Grace period to not overload
  Utilities.sleep(1000);
  return output;
}

in the cell that I want to paste the price, I use:

=importRegex("https://coinmarketcap.com/currencies/cosmos", "<div class=.priceValue___11gHJ.>(.*)<\/div>")

But instead of the price, I get the price plus a lot of junk at the end.

$14.18</div><span ... (lots of stuff after) 

Why isn't the regex working? Thank you!

chiwal
  • 49
  • 1
  • 7
  • 1
    Regular expressions tend to be things that you have to test and they are often sensitive to minor variations in data. – Cooper Feb 08 '21 at 17:28
  • 1
    So maybe instead of `.*` try `([$\\d.]{1,})` and possibly those double backslashes should just be one. I don't recall precisely. Or this might work as well `([^<]{1,})` Here's a [reference](https://github.com/google/re2/blob/master/doc/syntax.txt) – Cooper Feb 08 '21 at 17:34
  • Hi cooper that worked ! ([$\\d.]{1,}) and ([^<]{1,}) both worked / thank you so much! – chiwal Feb 08 '21 at 17:52
  • Related: https://stackoverflow.com/q/11306596/1595451, https://stackoverflow.com/q/27939085/1595451 – Rubén Feb 08 '21 at 18:28
  • Can someone tell me how to get rid of the dollar sign in Cooper's answer? it works and returns $13.99 for example, but I really need just 13.99. this works but with the dollar sign ---> importRegex("https://coinmarketcap.com/currencies/cosmos", "
    ([^<]{1,})<\/div>"
    – chiwal Feb 08 '21 at 19:47
  • Try `\$([\\d.]{1,})` I think the first backslash will let the expression know that you don't want the $ to be begining of line. And you could always look for $ and remove it with a slice() method – Cooper Feb 08 '21 at 20:07
  • @Cooper, thank you!, it worked perfectly – chiwal Feb 08 '21 at 23:02

1 Answers1

0

You should replace .* with \$([\\d.]{1,}).

ale13
  • 5,679
  • 3
  • 10
  • 25