Terminology

What is a Regular Expression?

Regular expressions, also known as “RegExp” or “RegEx”, are a powerful way to search in texts. It is a way to express in a textual way what you want to find in a given text. Valueble stuff for every developer. It is commonly used for validation of any kind of data like email addresses, date’s, postal codes etc…

You will find a simple regular expression C# example below:

using System.Text.RegularExpressions;
...
public void Test()
{
        string Value = "12345";
        string RegexPattern = "^[0-9]{5}$"
        bool Match = Regex.IsMatch(Value, RegexPattern); //Match = true;
}

As you can see, the variable “Value” is validated with the regular expression “RegexPattern”. The expression “^[0-9]{5}$” expresses the following:

  1. ”^” = from the very beginning of the “Value”… ,
  2. ”[0-9]” = “Value” must contain the character 0,1,2,3,4,5,6,7,8 or 9
  3. ”{5}” = for the next 5 character positions
  4. “$” = after that, no more characters are following.

What is an NPE?

An NPE stands for Null Pointer Exception. An NPE is one of the most common exceptions in most coding languages. It occurs for example in cases where a member of an object is called when the object isn’t there.

See the code example below

Person p = null;
WriteADebugLine(p);

void WriteADebugLine(Person p) {
    Debug.WriteLine(p.FirstName); //Exception
}

When p.FirstName is called, an NPE is raised because p is not inialized.

To prevent NPE’s it is a good habbit to assert before use.

WriteADebugLine(Person p) {
    if (p == null) {   // Assert
        return;
    }
    Debug.WriteLine(p.FirstName);
}