Home > Blockchain >  Using regex in Google Apps Script to extract number before a string
Using regex in Google Apps Script to extract number before a string

Time:05-05

I receive a Gmail that contains text similar to that below. I only want to extract the number immediately before the string 'symbol(s)'. In this case that number would be 233, but it and the other contents of the email vary every day.

ZBH Zimmer Biomet Holdings Inc Medical Devices Healthcare NYSE 25,250 120.68 0.03 0.02 3,124,139 ZBRA Zebra Technologies Corp Cl A Communication Equipment Technology NASDAQ 19,620 368.14 -11.43 -3.01 626,763 ZION Zions Bancorp N.A. Banks - Regional Financial Services NASDAQ 8,580 57.47 0.46 0.81 1,507,253 ZTS Zoetis Inc Drug Manufacturers - Specialty Healthcare NYSE 83,530 175.14 0.47 0.27 2,152,320 233 symbol(s)

I have figured out how to extract everything before,

(?:(?!symbols(s)).)*

but I don't want everything, only the number '233'.

Thank you

CodePudding user response:

Description

Regex is hard for most people to work with, myself included. Make your life easier. I would split the long text string into words and then search for "symbols(s)", then I know the number is just before that.

Script

function test2() {
  try { 
    let text = "ZBH Zimmer Biomet Holdings Inc Medical Devices Healthcare NYSE 25,250 120.68 0.03 0.02 3,124,139 ZBRA Zebra Technologies Corp Cl A Communication Equipment \
    Technology NASDAQ 19,620 368.14 -11.43 -3.01 626,763 ZION Zions Bancorp N.A. Banks - Regional Financial Services NASDAQ 8,580 57.47 0.46 0.81 1,507,253 ZTS Zoetis Inc \
    Drug Manufacturers - Specialty Healthcare NYSE 83,530 175.14 0.47 0.27 2,152,320 233 symbol(s)";
    text = text.split(" ");
    let i = text.indexOf("symbol(s)");
    console.log(text[i-1]);
  }
  catch(err) {
    console.log(err);
  }
}

Console.log

8:31:44 AM  Notice  Execution started
8:31:46 AM  Info    233
8:31:44 AM  Notice  Execution completed

Reference

CodePudding user response:

I like TheWizEd's approach, but in case you still want to try a regex, you can use the following:

\d*(?=\ssymbol)

It's called a lookahead assertion. It will match any number of digits before a space and the word "symbol".

If you really need to include the (s) at the end then you can use this one:

\d*(?=\ssymbol\(s\))

You can test it here with your sample text and it should work: https://regexr.com/

  • Related