2

I have 2 regular expressions which work perfectly on regex101, but, on the sheet script, one (REGEX_RANGO) returns false when calling .test, and the other (REGEX_INVIDIDUAL) doesn't work at all.

(Note: I use a cell on the sheet to debug this kind of situation, I don't know if its a better one, if someone knows, please, post it!)

My current regular expressions are:

var REGEX_RANGO = /((?=(\d|\,))(\d{1,3}-\d{1,3})+(?=(\s|\,)))/gm;
var REGEX_INDIVIDUAL = /((?<=,)|(?:^(\d)))[^(,|\-)\n]+((?=,)|(?:\s))/gm;

Why i need both? Well, I have a form, which people can join values and the regular expressions can work on them.

Inputs are like:

  • *000-005,100,200,250-275,300*:
    • REGEX_RANGO should get on the array the values [000,005,250,275]
    • REGEX_INDIVIDUAL on another array should get [100,200,300]
  • *001,002,003,010-015*:
    • REGEX_RANGO should get on the array the values [010,015]
    • REGEX_INDIVIDUAL should get [001,002,003]

Hope someone knows how to handle this, thanks. You can find my current attemps here and here.

To be clear: this works ok on regex101, not on Google Sheet, maybe a scope or i need to scape the regular expression?

EDITED:

var REGEX_INDIVIDUAL = /((?<=,)|(?:^(\d)))[^(,|\-)]+(?=($|,))/gm;
var j = numeros_ingresados.match( REGEX_INDIVIDUAL )
SpreadsheetApp.getActiveSheet( ).getRange("F1").setValue( " >> j : " + j )
12
  • Can you add your desired output in the post please?
    – JSmith
    Commented Sep 9, 2018 at 18:07
  • when I enter 000-005,100,200,250-275,300 with the REGEX_RANGO in regex101 I get the output you have with google app script
    – JSmith
    Commented Sep 9, 2018 at 18:09
  • Sorry, i edited it In regex101, it works, perfectly. The thing is on google sheet script, it doesn't work at all, same input returns false (nor returns nothing) using string.test(REGEX_RANGO) or string.test(REGEX_INDIVIDUAL) Thnx also to the admin who edited it, looks better!
    – lucas_7_94
    Commented Sep 9, 2018 at 18:19
  • can you write in your post the exact function you are using to make a match. Thx in advance
    – JSmith
    Commented Sep 9, 2018 at 18:26
  • 1
    Yes, I posted an answer that should be helpful. Commented Sep 9, 2018 at 19:06

1 Answer 1

2

Note that GAS JS RegExp does not support lookbehinds like (?<=,) in your pattern.

You may use the following sample code to extract the values you need:

function extractRangos() {
  var s = "000-005,100,200,250-275,300";
  var REGEX_RANGO = /(?:^|,)(\d+)-(\d+)(?![^,])/g;
  var REGEX_INDIVIDUAL = /(?:^|,)(\d+)(?![^,])/g;
  var m, res_rango = [], res_ind = [];
  while (m = REGEX_RANGO.exec(s)) {
    res_rango.push(m[1]);
    res_rango.push(m[2]);
  }
  while (m = REGEX_INDIVIDUAL.exec(s)) {
    res_ind.push(m[1]);
  }
  Logger.log(res_rango);
  Logger.log(res_ind);
}

Result log:

enter image description here

Regexp details

The individual regex pattern is

(?:^|,)(\d+)(?![^,])

It matches

  • (?:^|,) - start of a string or a comma
  • (\d+) - Capturing group 1: one or more digits
  • (?![^,]) - a negative lookahead that fails the match if the next char is not a comma (the next char after the digits should be a comma or end of string).

The point is to collect only Group 1 values.

See its online demo

The rango regex pattern is

(?:^|,)(\d+)-(\d+)(?![^,])

See this online demo

It matches:

  • (?:^|,) - a non-capturing group matching the start of string or a comma
  • (\d+) - Group 1: one or more digits
  • - - a hyphen
  • (\d+) - Group 2: one or more digits
  • (?![^,]) - a negative lookahead that fails the match if the next char is not a comma.

The point is to collect only Group 1 and 2 values.

5
  • Both modes works perfect! Thanks A note only: i replaced my REGEX_RANGO to /(?=(\d|,))(\d{1,3}-\d{1,3})+(?=($|,))/gm and works like a charm with .match, but really really thanks for the code!
    – lucas_7_94
    Commented Sep 9, 2018 at 19:09
  • 1
    @lucas_7_94 /(?=(\d|,))(\d{1,3}-\d{1,3})+(?=($|,))/gm is equal to /(\d{1,3}-\d{1,3})+(?=$|,)/gm as the (?=(\d|,)) requires the next char to be a digit or a comma, which is always true. Note the + after ) should be removed. Commented Sep 9, 2018 at 19:20
  • Something new learned, need to improve, thanks so much for the info!
    – lucas_7_94
    Commented Sep 9, 2018 at 19:33
  • This! Note that GAS JS RegExp does not support lookbehinds! I couldn't figure out why I couldn't get mine to work. Do you know where I can find more info on what is supported and what isn't? Commented Nov 5, 2018 at 20:14
  • 1
    @StevenKuipers Sorry, GAS JS RegExp is really somewhat crippled, there seem to be issues with non-capturing groups, too (can't remember the issue though). Here is some interesting article, but I don't know if there is anything more comprehensive. Commented Nov 5, 2018 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.