1

I tried the same regular expression both in python (3.6, jupyter notebook) and Google app script, but it seems like "non-capturing group" is not working in the app script case.

# python script:
import re
text='<a class=""email"" href=""mailto:[email protected]"">'
regex='(?:<a class=""email"" href=""mailto:)(.+?@hello\.edu)(?:"">)'
match=re.search(regex,text)
print(match.group(1))
# result is '[email protected]'

// Google app script
function myFunction() {
  string='<a class=""email"" href=""mailto:[email protected]"">'
  regex=new RegExp('(?:<a class=""email"" href=""mailto:)(.+?@hello\.edu)(?:"">)')
  Match=regex.exec(string)
  Logger.log(Match[1])
  // result is 'a class=""email"" href=""mailto:[email protected]'
}

If I am not mistaken, regular expression engine in Google app script should support non-capturing groups (referring to https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines, I suppose I should be looking at "JavaScript (ECMAScript)" and "Shy groups"?), can anyone explain what I'm missing here?

Thanks in advance!

0

1 Answer 1

1

First of all, you need to use \\ before . in the GAS regex declaration, as the literal backslash forms a regex escape sequence.

Now, it seems that GAS non-capturing group implementation is buggy.

If you run your regex in GAS and print the Match object, you will see

[18-01-26 08:49:07:198 CET] [<a class=""email"" href=""mailto:[email protected]"">, 
a class=""email"" href=""mailto:[email protected], "">]

That means, the non-capturing group got "merged" with the first capturing group skipping the first char.

Here are some more experiments:

Logger.log(new RegExp("(?:;\\w+):(\\d+)").exec(";er:34")); // = null, expected [;er:34, 34]
Logger.log(new RegExp("(?:e\\w+):(\\d+)").exec(";er:34")); // = null, expected [er:34, 34]
Logger.log(new RegExp("(?:\\w+):(\\d+)").exec(";er:34"));  // =  [er:34, 34], as expected

To fix the issue, you may remove the non-capturing parentheses, as \d = (?:\d).

6
  • Oh I just realized \w has to be \\w in GAS! Thanks a lot for pointing that out! May I ask why is that (is there any convention / backend engine related reason?)? (somehow it contradicts with support.google.com/a/answer/1371417?hl=en)
    – Matthew
    Jan 26, 2018 at 15:43
  • But somehow both Logger.log(new RegExp("\\.").exec(".")); and Logger.log(new RegExp("\.").exec(".")); Match the dot! This is very confusing!
    – Matthew
    Jan 26, 2018 at 15:46
  • @Matthew A regex escape sequence is a combination of a literal backslash and some symbol(s) after it. A "\w" is a w because a single backslash is used to define escape sequences like \n (newline), \r (carriage return), \t (tab), etc. Thus, you must use \\w to match any word char and not w. A "\." is equal to ".", and a dot matches any char but a line break char, thus both "\\." and "\." match a dot, but "\\." will never match § and "\." will. Jan 26, 2018 at 15:57
  • Thanks a lot for your clarification! So it seems like all the \ (as in python case) should be replaced with \\ in GAS? Is there any particular background reason (convention / backend engine related) for this difference? To illustrate such differences, Logger.log(new RegExp('\w+').exec('abcsw')); matches w, Logger.log(new RegExp('\\w+').exec('abcsw')); matches abcsw
    – Matthew
    Jan 26, 2018 at 16:40
  • And thanks for your answer again! It really helps! By removing the non-capturing parenthesis, it works now! string='<a class=""email"" href=""mailto:[email protected]"">' regex=new RegExp('<a class=""email"" href=""mailto:(.+?@hello\\.edu)"">') Match=regex.exec(string) Logger.log(Match[1]) // return '[email protected]'
    – Matthew
    Jan 26, 2018 at 16: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.