Regex 正则表达式问题评估特殊字符
我有一个字段用于搜索现有资产或创建新资产 搜索只使用资产id 例如 1234 并且create包含一些逗号分隔的额外字段 e、 g 因此,我使用的正则表达式如下所示(看起来工作正常)Regex 正则表达式问题评估特殊字符,regex,Regex,我有一个字段用于搜索现有资产或创建新资产 搜索只使用资产id 例如 1234 并且create包含一些逗号分隔的额外字段 e、 g 因此,我使用的正则表达式如下所示(看起来工作正常) ((\w+)(,\w+){3})|^\w+ 我的问题是,有时资产id可以连字符 例如 克隆1-12 我已尝试将我的正则表达式更新为 ((\w+\-?\w*)(,\w+){3})|^\w+ 但它似乎没有处理连字符。还有什么我应该做的吗 帮助澄清 它遵循一种格式 AssetID,序列号,零件号,型号 因此,一个文
((\w+)(,\w+){3})|^\w+
我的问题是,有时资产id可以连字符
例如
克隆1-12
我已尝试将我的正则表达式更新为
((\w+\-?\w*)(,\w+){3})|^\w+
但它似乎没有处理连字符。还有什么我应该做的吗
帮助澄清 它遵循一种格式 AssetID,序列号,零件号,型号 因此,一个文本框验证条目是否仅为assetID或是否为assetID、序列号、零件号、型号
其他字段在布局上是非常静态的(总是字母数字的,没有特殊字符),只是assetid可以包含连字符,正则表达式的第二个分支仍然是
^\w+
。您可能应该将其更改为^[\w-]+
正确的正则表达式应该是:([\d\-]+)+
它将把所有数字+连字符作为单独的组,而不管有多少个m
此外,您还可以使用联机检查regexp,并获得每个符号的详细说明
编辑:如果您想知道,下面是关于我的regexp版本的解释:
1st Capturing Group ([\d\-]+)+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
*A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data*
Match a single character present in the list below [\d\-]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equal to [0-9])
\- matches the character - literally (case sensitive)
您是否尝试过反斜杠连字符?它到底是如何工作的?它不匹配,所以很抱歉我应该指定,id可以是字母数字连字符,例如clone1-1。但是在创建新资产时,条形码必须与字母数字、字母数字、字母数字的格式相匹配。@Damage,而不是像这样的格式我想:
([a-zA-Z0-9\-]+)+| ^[a-zA-Z0-9\-]+
,其中a-Z
部分是可选的(取决于您是否允许使用大写字符)奇怪的([a-zA-Z0-9\-]+)(,\w+{3})|^\w+它将匹配此克隆11-123123,但不匹配克隆11-123@Damage,因为克隆11-123与第一个备选方案([a-zA-Z0-9\-]+)+(,\w+{3})不匹配,而第二个备选方案-^\w+
表示任意数量的字母数字字符,但没有其他符号。请尝试([a-zA-Z0-9\-])+(,\w+){3} )|^[\w\-]+
我最初误读了那篇评论。谢谢罗兰,我最终得到了((\w+\-?\w*)(,\w+{3})^(\w+\-?\w*))$
1st Capturing Group ([\d\-]+)+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
*A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data*
Match a single character present in the list below [\d\-]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equal to [0-9])
\- matches the character - literally (case sensitive)