regex - Google 表单正则表达式 (REGEX) 逗号分层 (CSV)
问题描述
我有一个包含 1 个或多个 ID 的 Google 表单字段
图案:
- ID 始终为 6 个数字。
- 如果只输入一个 ID,则不需要逗号和空格。
- 如果输入了多个 ID,则需要逗号和空格。
- 如果输入了多个 ID,则最后一个 ID 的末尾不应有逗号或空格。
允许的例子:
- 单号:123456
- 多个ID:123456、456789、987654
这是我当前的正则表达式(无法正常工作)
[0-9]{6}[,\s]?([0-9]{6}[,\s])*[0-9]?
我究竟做错了什么?
解决方案
使用您显示的示例,您能否尝试以下操作。
^((?:\d{6})(?:(?:,\s+\d{6}){1,})?)$
说明:添加上述正则表达式的详细说明。
^( ##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.
推荐阅读
- ios - 为什么 decrytedText 为 NULL?
- android - TYPE_STATUS_BAR 覆盖使系统忽略屏幕超时
- conditional-formatting - 根据另一张纸上的单元格输入条件格式化 1 个单元格,应用于整行
- vmware-clarity - 数据网格行选择计数不正确
- docusignapi - 通过 REST 添加信封附件
- php - Xdebug 和 PhpStorm - 调试不起作用
- php - 按类别分组数组
- asp.net - 带有 group by 的 desc 顺序在 linq 中不起作用
- concatenation - 在 SPARQL 查询 (MarkLogic) 中用作主题的连接值
- php - 尝试 cURLOPt_FOLLOWLOCATION = true 在 cURL 之后强制标头功能,但没有任何效果