C# Star (Regex Syntax, Kleene Closure)This C# program uses the star character in a Regex pattern. Star means zero or more.
Regex star. A star means zero or more characters. The star—also called an asterisk or Kleene closure—provides a way to specify an optional character or grouping of characters.
This Regex notation is universal and important. We can use the star in C# Regex patterns. This makes many advanced patterns possible.
This example uses the star character. The star (or asterisk) character is useful in many implementations. It is also found in many language specifications.
Tip: The star indicates that zero or more matches of the preceding group must be found in the string for the result to be true.
So: You can use the star to specify optional characters. Zero matches is still allowed.
And: If you use the star on a group in parenthesis, the entire group is treated as optional and repeatable.
C# program that uses star in regular expressions
static void Main()
// Use the star to indicate zero or more instances of the character.
// ... The period indicates any character.
Regex regex = new Regex("dotnet.*");
Console.WriteLine(regex.IsMatch("dotnetperls")); // True
// Zero or more s letters are allowed.
regex = new Regex("perls*");
Console.WriteLine(regex.IsMatch("perl")); // True
Console.WriteLine(regex.IsMatch("perlssss")); // True
Console.WriteLine(regex.IsMatch("pert")); // False
// Use the star with a parentheses grouping.
// ... Also use the $ sign to signal the terminal.
regex = new Regex("sam(abc)*$");
Console.WriteLine(regex.IsMatch("samabcabc")); // True
Console.WriteLine(regex.IsMatch("sam")); // True
Console.WriteLine(regex.IsMatch("samab")); // False
Notes, above program.
We use the star character in regular expressions. The star (asterisk) means that zero or more of the preceding character group should be matched.
Note: This means if nothing matches the character group, the regular expression can still match based on other data.
The result value from the IsMatch instance method is printed to the console when it is invoked on various Regex instances with various parameters.
Next: The first IsMatch call returns True, and this is because the period character matches any character.
So: The ".*" notation indicates zero or more of any character. This matches the trailing characters in the parameter string.
A star is also called a Kleene closure. You can use a parenthetical grouping and apply the star to that. The entire group must be repeated zero or more times.
Theory: The star is part of several symbols that can describe languages. In compiler theory, regular expressions specify regular languages.
And: Regular expression notation can describe tokens by describing the lexemes that make up those tokens.
We looked at the star character in regular expression syntax. The star or asterisk character means that zero or more instances should be matched in the string being tested.Regex
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.