JKennedy
JKennedy

Reputation: 18799

Issue with regex property equaling {NULL}

I have a regex that I am trying to evaluate the value of in the following method:

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

Where:

    private Regex ValidationRegex { get; set; }

Now, when it gets to this.ValidationRegex != null I hover over the ValidationRegex value and {NULL} is displayed, so I expect my line to return false and go into the else statement.

Instead, this line returns true and I can't understand why. I have attached a screenshot of this below:

enter image description here

I can't seem to work it out (This is not a threading thing)

Upvotes: 1

Views: 285

Answers (2)

Paprik
Paprik

Reputation: 159

According to the documentation, the ToString() of Regex class (which is what Visual Studio is displaying) returns the regular expression pattern that was passed into the Regex constructor.

So I would assume your pattern is NULL.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627022

The pattern you have in the Regex object is NULL.

var ValidationRegex = new Regex("NULL");

This regex is looking for a string NULL in the input string (like This value is NULL.), and the ValidationRegex object is initialized and is not null. So, if evaluates to true.

Just a note: you can't check if a value is NULL using regex.

Upvotes: 2

Related Questions