This sample is a modified version from here
Created
November 28, 2024 11:46
-
-
Save karenpayneoregon/f710f44ab8f4332017057daa22efff7e to your computer and use it in GitHub Desktop.
NET 9 [GeneratedRegex] on properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
namespace Net9Features; | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string sentence = "a test to see if"; | |
Console.WriteLine(Helpers.ContainsFiveCharWord(sentence)); | |
Console.ReadLine(); | |
} | |
} | |
public static partial class Helpers | |
{ | |
/// <summary> | |
/// Determines whether the specified input string contains a word with exactly five characters. | |
/// </summary> | |
/// <param name="input">The input string to be checked for a five-character word.</param> | |
/// <returns> | |
/// <c>true</c> if the input string contains a word with exactly five characters; otherwise, <c>false</c>. | |
/// </returns> | |
public static bool ContainsFiveCharWord(string input) => | |
!string.IsNullOrEmpty(input) && FiveCharWord.IsMatch(input); | |
[GeneratedRegex(@"^\s*\b\w+\b(\s+\b\w+\b){4}\s*$")] | |
private static partial Regex FiveCharWord { get; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment