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