Difference of regex in python and google app script (backend engine related?)

前端 未结 1 1778
小鲜肉
小鲜肉 2021-01-16 07:48

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

相关标签:
1条回答
  • 2021-01-16 08:26

    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:SOisAwesome@hello.edu"">, 
    a class=""email"" href=""mailto:SOisAwesome@hello.edu, "">]
    

    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).

    0 讨论(0)
提交回复
热议问题