正则表达式问题评估特殊字符

时间:2017-02-06 21:50:52

标签: regex

我有一个字段用于搜索现有资产或创建新资产

搜索仅使用资产ID 例如 1234

并且create包含一些以逗号分隔的额外字段 e.g

1234,5678,9101112,131415

因此正在使用的正则表达式在下面(它看起来正常工作

((\w+)(,\w+){3})|^\w+

我的问题偶尔是资产ID可以连字符 例如 clone1-12

我已尝试将我的正则表达式更新为

((\w+\-?\w*)(,\w+){3})|^\w+

但它似乎没有处理连字符。还有其他我应该做的事吗?

帮助澄清

它遵循一种格式 AssetID,序列号,部件号,型号

因此,一个文本框验证该条目只是assetID,或者是AssetID,序列号,部件号,型号否

其他字段在布局上非常静态(总是字母数字且没有特殊字符),它只是可以包含连字符的assetid

2 个答案:

答案 0 :(得分:0)

正则表达式的第二个分支仍为^\w+。您应该将其更改为^[\w-]+

答案 1 :(得分:0)

正确的正则表达式为:([\d\-]+)+ 它将所有数字+连字符作为单独的组,无论有多少m。

此外,您可以使用this resource在线检查您的正则表达式,并获得每个符号的详细说明。

编辑:如果你想知道,这是我的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)