Google Forms 正则表达式 (REGEX) 逗号分隔 (CSV)

Jam*_*d68 2 regex google-forms

我有一个 Google 表单字段,其中包含 1 个或多个 ID

图案:

  • ID 总是 6 个数字。
  • 如果只输入一个 ID,则不需要逗号和空格。
  • 如果输入多个 ID,则需要一个逗号和一个空格。
  • 如果输入了多个 ID,则最后一个 ID 的末尾不应有逗号或空格。

允许的例子:

  • 单个ID:123456
  • 多个 ID:123456、456789、987654

这是我当前的 REGEX(无法正常工作)

[0-9]{6}[,\s]?([0-9]{6}[,\s])*[0-9]?

我究竟做错了什么?

Rav*_*h13 6

使用您显示的样本,您能否尝试以下操作。

^((?:\d{6})(?:(?:,\s+\d{6}){1,})?)$
Run Code Online (Sandbox Code Playgroud)

上述正则表达式的在线演示

说明:添加上述正则表达式的详细说明。

^(                     ##Checking from starting of value, creating single capturing group.
   (?:\d{6})           ##Checking if there are 6 digits in a non-capturing group here.
   (?:                 ##Creating 1st non-capturing group here
      (?:,\s+\d{6})    ##In a non-capturing group checking it has comma space(1 or more occurrences) followed by 6 digits here.
   ){1,})?             ##Closing 1st non-capturing group here, it could have 1 or more occurrences of it.
)$                     ##Closing 1st capturing group here with $ to make sure its end of value.
Run Code Online (Sandbox Code Playgroud)

  • 啊! 我明白混乱在哪里了。那是我的错。我已从“如果仅输入 1 个 ID,则不需要逗号和空格”更新了我的问题。改为“如果只输入一个 ID,则不需要逗号和空格。” (2认同)