【问题标题】:What is the Google Apps Script equivalent of Python's re.fullmatch?与 Python 的 re.fullmatch 等效的 Google Apps 脚本是什么?
【发布时间】:2020-04-27 03:29:55
【问题描述】:

我在 python 中编写了一个脚本,它的功能完全符合我的需要,但它看起来不像 Google Apps 脚本在文档中具有正则表达式完全匹配方法。本质上,我有一个用户输入的字符串,我想验证该字符串是否完全匹配正则表达式; “完全匹配”。我对 Google Apps 脚本非常熟悉,可以将 python 脚本转换为 GAS,但需要有关工作流程的帮助,并且可能针对此给定的 GAS 采用不同的逻辑方法似乎没有完全匹配可用。下面的代码是python脚本供参考。

import re

def is_valid(text, valid_styles):

    good_string_found = re.fullmatch(r'(([A-Z]+)\s([0-9]+)\s-\s([0-9]+)x?;\n)+', text)

    if good_string_found is None:
        return False, 1

    for each_item in text.split(";\n")[: len(text.split(";\n")) - 1]:

        style_number, size = each_item.split(" - ")[0], each_item.split(" - ")[1]
        valid_sizes = ["4","5","6","6x","7","8","10","12","14","16"]

        if style_number not in valid_styles or size not in valid_sizes:
            print("First error found in: " + style_number + " - " + size)
            return False, 2

    return True, 0

user_entry = "CS 101 - 8;\nCS 102 - 10;\nCS 104 - 6x;\n"
valid_style_numbers = ["CS 101", "CS 102", "CS 103", "CS 104"]
validity, error = is_valid(user_entry, valid_style_numbers)

if validity is False and error == 1:
    print("User entered string did not:\n- Match regex expected input")
elif validity is False and error == 2:
    print("User entered string did not:\n- Contain a valid style number\n- Contain a valid size")
elif validity is True:
    print("Proper scan")

【问题讨论】:

  • GAS 只是 JavaScript

标签: python regex google-apps-script


【解决方案1】:

我相信你的目标如下。

  • 您想将 Python 脚本转换为 Google Apps 脚本。

对于这个,这个答案怎么样?

在这个答案中,为了检查text 的完全匹配,使用了^(([A-Z]+)\s([0-9]+)\s-\s([0-9]+)x?;\n)+$ 的正则表达式。而test就是用于这种情况的。

示例脚本 1:

这是完整匹配的测试脚本。

const text1 = "CS 101 - 18;\nCS 102 - 10;\nCS 104 - 6x;\n";
const res1 = (/^(([A-Z]+)\s([0-9]+)\s-\s([0-9]+)x?;\n)+$/).test(text1);
console.log(res1)

const text2 = "CS 1foo01 - 18;\nCS 102 - 10;\nCS 104 - 6x;\n";
const res2 = (/^(([A-Z]+)\s([0-9]+)\s-\s([0-9]+)x?;\n)+$/).test(text2);
console.log(res2)
  • 我觉得^([A-Z]+\s[0-9]+\s-\s[0-9]+x?;\n)+$也可以用。

示例脚本 2:

当您的 python 脚本转换为 Google Apps Script 时,它变成如下。

function is_valid(text, valid_styles) {
  const good_string_found = (/^(([A-Z]+)\s([0-9]+)\s-\s([0-9]+)x?;\n)+$/).test(text);
  if (!good_string_found) return [false, 1];
  let items = text.split(";\n");
  items.pop();
  for (let i = 0; i < items.length; i++) {
    const each_item = items[i];
    const [style_number, size] = [each_item.split(" - ")[0], each_item.split(" - ")[1]];
    const valid_sizes = ["4","5","6","6x","7","8","10","12","14","16"];
    if (!valid_styles.includes(style_number) || !valid_sizes.includes(size)) {
      console.log("First error found in: " + style_number + " - " + size);
      return [false, 2];
    }
  }
  return [true, 0];
}

// Please run this function.
function main() {
  const user_entry = "CS 101 - 18;\nCS 102 - 10;\nCS 104 - 6x;\n";
  const valid_style_numbers = ["CS 101", "CS 102", "CS 103", "CS 104"];
  const [validity, error] = is_valid(user_entry, valid_style_numbers);
  if (!validity && error == 1) {
    console.log("User entered string did not:\n- Match regex expected input");
  } else if (!validity && error == 2) {
    console.log("User entered string did not:\n- Contain a valid style number\n- Contain a valid size")
  } else if (validity) {
    console.log("Proper scan")
  }
}

参考:

【讨论】:

  • 这太棒了,稍微修改了一下,但效果很好。看到别人的代码很有趣,就像我从未使用过“const”或“let”一样。为了让它在我的 .gs 文件中工作,我必须将 const 切换为 var,从 for 循环中删除 let,而不是 .includes,我需要使用 indexOf() == -1。您的 .test 运行良好,正是我想要的!
  • @Lucas Bassoli 感谢您的回复。很高兴您的问题得到解决。
猜你喜欢
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多