首页 > 解决方案 > ConfigurationSectionDesigner验证中分号分隔数字字符串的正则表达式?

问题描述

需要对带有分号分隔的 HTTP 状态代码的字符串应用正则表达式验证:

"202;500;503"

我试过这个正则表达式

^([0-9]+;)*[0-9]+$

但我得到一个例外:

属性“retryStatusCodes”的值无效。错误是:该值不符合验证正则表达式字符串^([0-9]+;)*[0-9]+$

这怎么可能?我在这里测试过:https ://regexr.com/

它奏效了

为什么它在 C# 代码中不起作用?

我用它来验证 ConfigurationSectionDesigner 中的属性,该属性的自动生成代码:

    /// <summary>
    /// Gets or sets the RetryStatusCodes.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.3.2")]
    [global::System.ComponentModel.DescriptionAttribute("The RetryStatusCodes.")]
    [global::System.Configuration.RegexStringValidatorAttribute("^([0-9]+;)*[0-9]+$")]
    [global::System.Configuration.ConfigurationPropertyAttribute(global::XXX.XXX.XXX.RetryStatusCodesPropertyName, IsRequired=true, IsKey=false, IsDefaultCollection=false)]
    public virtual string RetryStatusCodes
    {
        get
        {
            return ((string)(base[global::XXX.XXX.XXX.RetryStatusCodesPropertyName]));
        }
        set
        {
            base[global::XXX.XXX.XXX.RetryStatusCodesPropertyName] = value;
        }
    }

标签: c#regexconfigurationsection

解决方案


推荐阅读