How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Declare the regex pattern
        string pattern = @"htt+p";
        Regex regex = new Regex(pattern);

        // Test strings
        string[] testStrings = { "http", "htttp", "httttp", "httpp", "htp" };

        // Check matches
        foreach (string test in testStrings)
        {
            bool match = regex.IsMatch(test);
            Console.WriteLine($"Matches \"{test}\": {match}");
        }
    }
}

// Matches "httpp": True or false, depending on how matches() method works


/*
run:

Matches "http": True
Matches "htttp": True
Matches "httttp": True
Matches "httpp": True
Matches "htp": False

*/

 



answered May 15, 2025 by avibootz
...