Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created November 28, 2024 11:46
Show Gist options
  • Save karenpayneoregon/f710f44ab8f4332017057daa22efff7e to your computer and use it in GitHub Desktop.
Save karenpayneoregon/f710f44ab8f4332017057daa22efff7e to your computer and use it in GitHub Desktop.
NET 9 [GeneratedRegex] on properties

This sample is a modified version from here

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