正则表达式问题 属性 等于 {NULL}

Issue with regex property equaling {NULL}

我有一个正则表达式,我正在尝试使用以下方法评估其值:

    private void ValidateText()
    {
        if ((!IsOptional && Text == string.Empty))
        {
            SetIsValid(false);
        }
        else
        {
            if (this.ValidationRegex != null)
            {
                SetIsValid(this.ValidationRegex.IsMatch(this._text));
            }
            else
            {
                SetIsValid(true);
            }
        }
    }

其中:

    private Regex ValidationRegex { get; set; }

现在,当它到达 this.ValidationRegex != null 时,我将鼠标悬停在 ValidationRegex 值上并显示 {NULL},因此我希望我的行变为 return false 并进入 else 语句。

相反,这一行 returns true 我不明白为什么。我附上了下面的截图:

我似乎无法解决(这不是线程问题)

您在 Regex 对象中的模式是 NULL

var ValidationRegex = new Regex("NULL");

这个正则表达式在输入字符串中寻找字符串NULL(如This value is NULL.),并且ValidationRegex对象被初始化并且不是 空。因此,if 的计算结果为 true

请注意:您无法使用正则表达式检查值是否为 NULL。

According to the documentation,Regex class 的 ToString()(这是 Visual Studio 显示的内容)returns 传递到 Regex 构造函数中的正则表达式模式.

所以我假设你的模式是 NULL。