regex - Regex comma delimited string with specific characters
问题描述
I'm looking for a pattern that will match each character in a comma delimited string and make sure it is one of a specific set of characters
Specific set of characters = A, B, C
I'm pretty new to regex so it would be nice if you could break down how this works as well please :)
Example
The following strings should match
A
A,B
A,B,C,B,A,B,C
The following strings should not match
D
A,D
A,B,C,B,B,A,D
解决方案
如果您只想匹配整个字符串(而不是其中的任何组),您可以简单地匹配包含任意数量的指定字符和逗号的字符串:
^[ABC][ABC,]*$
编辑:如果您需要确保不匹配双字母(例如,A,B,CC, A
不应该匹配),那么您可以对该特定模式进行分组:
^[ABC](,[ABC])*$
推荐阅读
- terminology - 当一组四个十六进制字符(8 列)时,数据格式是什么?
- heroku - 在已部署的 heroku 应用程序中找不到上传的媒体
- r - group_by 和 pmap 对每组的每一行进行分段操作(ifelse vs case_when)
- html - 如何使输入不可见但不隐藏?
- reactjs - 使用 ag 网格反应功能组件无法通过上下文调用父函数
- ios - Xcode Bot 错误“不再支持已配置的目标...”
- android - Android系统的DropBoxManager最大尺寸是多少?
- matplotlib - Matplotlib 问题:x 标签彼此重叠。如何隔开 x 标签以提高可读性
- google-apps-script - 每次添加新行时,尝试阻止我的邮件合并功能从第 2 行运行
- mongoose - Mongoose findOneAndUpdate() 深层嵌套结构