Leaders Logo

Practical Applications of Regular Expressions for Validation in Csharp

Introduction to Regular Expressions

Regular expressions (regex) are a powerful tool for working with text patterns. They allow for efficient and flexible searching, replacing, and validating, and are widely used in programming languages such as C#. In this article, we will explore some practical applications of regular expressions for data validation in C#.

Regular expressions are used in various areas of programming, from validating user inputs to analyzing large volumes of textual data. The ability to define complex patterns concisely makes regular expressions an indispensable tool for developers who want to ensure data integrity in their applications.

Understanding how regular expressions work and how to apply them correctly in C# can save time and reduce the amount of code needed to perform common validation tasks. Throughout this article, we will see practical examples illustrating how to validate different types of data, such as email addresses, phone numbers, CPF, URLs, passwords, dates, postal codes, and much more.

Email Address Validation

Email address validation is one of the most common applications of regular expressions. A valid email must follow a specific pattern that includes alphanumeric characters, dots, and the '@' symbol. Below is an example of how to validate an email address using C#.


using System;
using System.Text.RegularExpressions;

public class EmailValidator
{
    public static void Main()
    {
        string email = "example@domain.com";
        string pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
        
        bool isValid = Regex.IsMatch(email, pattern);
        Console.WriteLine($"Is the email '{email}' valid? {isValid}");
    }
}

The regex pattern used here, ^[^@\s]+@[^@\s]+\.[^@\s]+$, works as follows (MOZILLA, 2025):

  • ^: Indicates the start of the string.
  • [^@\s]+: One or more characters that are not '@' or spaces.
  • @: The '@' character that separates the name from the domain.
  • [^@\s]+: Again, one or more characters that are not '@' or spaces, representing the domain.
  • \.: A dot that separates the domain from the extension.
  • [^@\s]+: One or more characters that are not spaces or '@', representing the domain extension.
  • $: Indicates the end of the string.

Phone Number Validation

Validating phone numbers can be challenging due to different formats. A regular expression can be created to accept various formats. The example below validates phone numbers in the format (xx) xxxx-xxxx or (xx) xxxxx-xxxx.


using System;
using System.Text.RegularExpressions;

public class PhoneValidator
{
    public static void Main()
    {
        string phone = "(12) 3456-7890";
        string pattern = @"^\(\d{2}\) \d{4,5}-\d{4}$";
        
        bool isValid = Regex.IsMatch(phone, pattern);
        Console.WriteLine($"Is the phone '{phone}' valid? {isValid}");
    }
}

The regular expression ^\(\d{2}\) \d{4,5}-\d{4}$ works as follows:

  • ^: Start of the string.
  • \(\d{2}\): Two digits in parentheses for the area code.
  • : A space.
  • \d{4,5}: Four or five digits, representing the phone number.
  • -: A dash that separates the phone number from the rest.
  • \d{4}: Four final digits of the phone number.
  • $: End of the string.

CPF Validation

In Brazil, the CPF (Cadastro de Pessoas Físicas) is an important document, and its validation is essential. Below is an example of how to validate a CPF using regular expressions, considering the formatting with dots and dashes (ALURA, 2025).


using System;
using System.Text.RegularExpressions;

public class CPFValidator
{
    public static void Main()
    {
        string cpf = "123.456.789-09";
        string pattern = @"^\d{3}\.\d{3}\.\d{3}-\d{2}$";
        
        bool isValid = Regex.IsMatch(cpf, pattern);
        Console.WriteLine($"Is the CPF '{cpf}' valid? {isValid}");
    }
}

The regular expression ^\d{3}\.\d{3}\.\d{3}-\d{2}$ is composed of:

  • ^: Start of the string.
  • \d{3}: Three digits for the first block of the CPF.
  • \.: A dot.
  • \d{3}: Three digits for the second block of the CPF.
  • \.: Another dot.
  • \d{3}: Three digits for the third block of the CPF.
  • -: A dash.
  • \d{2}: Two digits that form the final part of the CPF.
  • $: End of the string.

URL Validation

Validating URLs is another common task. A regular expression can be used to check if a string is a valid URL. The example below validates URLs that start with http:// or https://.


using System;
using System.Text.RegularExpressions;

public class URLValidator
{
    public static void Main()
    {
        string url = "https://www.example.com";
        string pattern = @"^(http|https)://[^\s/$.?#].[^\s]*$";
        
        bool isValid = Regex.IsMatch(url, pattern);
        Console.WriteLine($"Is the URL '{url}' valid? {isValid}");
    }
}

The regular expression ^(http|https)://[^\s/$.?#].[^\s]*$ is explained as follows:

  • ^: Start of the string.
  • (http|https): The protocol of the URL, which can be 'http' or 'https'.
  • ://: Literal indicating the start of the address after the protocol.
  • [^\s/$.?#]: Any character that is not whitespace or one of the mentioned special characters, starting the domain.
  • .: Any character after the domain.
  • [^\s]*: Any sequence of characters that does not contain whitespace, allowing for capturing parameters and paths.
  • $: End of the string.

Password Validation

Password validation is crucial for security. A strong password may require a combination of uppercase letters, lowercase letters, numbers, and special characters. The following example validates a password with at least 8 characters, including at least one uppercase letter, one lowercase letter, one number, and one special character.


using System;
using System.Text.RegularExpressions;

public class PasswordValidator
{
    public static void Main()
    {
        string password = "Password@123";
        string pattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$";
        
        bool isValid = Regex.IsMatch(password, pattern);
        Console.WriteLine($"Is the password '{password}' valid? {isValid}");
    }
}

The regular expression ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ is a bit more complex and can be detailed as follows:

  • ^: Start of the string.
  • (?=.*[a-z]): Asserts that the string contains at least one lowercase letter.
  • (?=.*[A-Z]): Asserts that the string contains at least one uppercase letter.
  • (?=.*\d): Asserts that the string contains at least one digit.
  • (?=.*[@$!%*?&]): Asserts that the string contains at least one special character.
  • [A-Za-z\d@$!%*?&]{8,}: Allows any combination of letters, numbers, and special characters, with a minimum of 8 characters.
  • $: End of the string.

Date Validation

Validating dates can be complex, especially when dealing with different formats. The regular expression below validates dates in the format DD/MM/YYYY, ensuring that the days and months are valid.


using System;
using System.Text.RegularExpressions;

public class DateValidator
{
    public static void Main()
    {
        string date = "31/12/2023";
        string pattern = @"^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}$";
        
        bool isValid = Regex.IsMatch(date, pattern);
        Console.WriteLine($"Is the date '{date}' valid? {isValid}");
    }
}

The regular expression ^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}$ is divided into parts:

  • ^: Start of the string.
  • (0[1-9]|[12][0-9]|3[01]): Can be a two-digit number representing the day, where:
    • 0[1-9]: Days from 01 to 09.
    • [12][0-9]: Days from 10 to 29.
    • 3[01]: Days 30 and 31.
  • /: Separator between day and month.
  • (0[1-9]|1[0-2]): Represents the month, where:
    • 0[1-9]: Months from 01 to 09.
    • 1[0-2]: Months 10 to 12.
  • /\d{4}: Four digits representing the year.
  • $: End of the string.

Postal Code Validation

Postal codes can vary in format depending on the country. In Brazil, the format is 00000-000. Below is an example of postal code validation using regular expressions.


using System;
using System.Text.RegularExpressions;

public class PostalCodeValidator
{
    public static void Main()
    {
        string postalCode = "12345-678";
        string pattern = @"^\d{5}-\d{3}$";
        
        bool isValid = Regex.IsMatch(postalCode, pattern);
        Console.WriteLine($"Is the postal code '{postalCode}' valid? {isValid}");
    }
}

The regular expression ^\d{5}-\d{3}$ can be analyzed as follows:

  • ^: Start of the string.
  • \d{5}: Five digits representing the first part of the postal code.
  • -: A dash that separates the two parts of the postal code.
  • \d{3}: Three digits representing the second part of the postal code.
  • $: End of the string.

Integer and Decimal Number Validation

Validation of integer and decimal numbers can also be performed with regular expressions. The example below validates numbers that can have a plus or minus sign and can be either integers or decimals.


using System;
using System.Text.RegularExpressions;

public class NumberValidator
{
    public static void Main()
    {
        string number = "-123.456";
        string pattern = @"^[+-]?\d+(\.\d+)?$";
        
        bool isValid = Regex.IsMatch(number, pattern);
        Console.WriteLine($"Is the number '{number}' valid? {isValid}");
    }
}

The regular expression ^[+-]?\d+(\.\d+)?$ is composed of:

  • ^: Start of the string.
  • [+-]?: An optional plus or minus sign.
  • \d+: One or more digits representing the integer part of the number.
  • (\.\d+)?: An optional group representing the decimal part, starting with a dot.
  • $: End of the string.

Conclusion

Regular expressions are a powerful tool for data validation in C#. They allow for creating efficient and flexible solutions for a variety of input formats. Through the examples presented, it is possible to see how regular expressions can simplify the validation process, making it safer and more reliable.

It is essential, however, that developers understand the limitations and challenges associated with using regular expressions, ensuring that they are applied correctly in their projects. Although regular expressions can be very useful, they can also become complex and difficult to understand, especially when trying to validate data that has many possible variations.

Additionally, it is important to consider that validation through regular expressions should not be the only line of defense in an application. Other forms of validation and data sanitization should be implemented, especially when dealing with user inputs that may be maliciously manipulated. The use of regular expressions should be part of a comprehensive approach to data security and integrity.

Finally, when implementing validations in your applications, it is advisable to extensively test the regular expressions to ensure that all desired use cases are adequately handled, avoiding false positives and negatives. This not only improves the reliability of the system but also provides a better user experience.

References

  • MOZILLA FOUNDATION. Regular Expressions: Cheatsheet. Available at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Cheatsheet. Accessed on: January 1, 2025.
  • ALURA. Regex in C#: how to use regular expressions. Available at: https://www.alura.com.br/artigos/regex-c-sharp-utilizar-expressoes-regulares. Accessed on: January 1, 2025.
About the author