| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RegexExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Text.RegularExpressions; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Text; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class RegexExtensions |
| | | 15 | | { |
| | | 16 | | extension(string? value) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Validates a string against the specified regular expression pattern. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="pattern">The regex pattern to match against.</param> |
| | | 22 | | /// <returns><c>true</c> if the string matches the pattern; otherwise, <c>false</c>.</returns> |
| | | 23 | | public bool Matches(Regex pattern) |
| | | 24 | | { |
| | 24 | 25 | | ArgumentNullException.ThrowIfNull(pattern); |
| | | 26 | | |
| | 21 | 27 | | return !string.IsNullOrEmpty(value) && pattern.Match(value).Length > 0; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Determines whether the string is a valid email address. The method validates against a precompiled email pat |
| | | 32 | | /// </summary> |
| | | 33 | | /// <returns><c>true</c> if the string is a valid email address; otherwise, <c>false</c>.</returns> |
| | 6 | 34 | | public bool IsEmailAddress() => value.Matches(RegexPatterns.EmailPattern); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Determines whether the string is a valid phone number. The method validates against a precompiled phone patt |
| | | 38 | | /// </summary> |
| | | 39 | | /// <returns><c>true</c> if the string is a valid phone number; otherwise, <c>false</c>.</returns> |
| | 6 | 40 | | public bool IsPhoneNumber() => value.Matches(RegexPatterns.PhonePattern); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |