1

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)

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well spotted. This actually turns out to be me translating NULL from my sqlite database to "NULL" and not null like it should. Thanks
Yes, sqlite is known for that kind of behavior. You need to cast the returned strings to their original types. If you need more help with this, please feel free to drop a comment.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.