C# Star: Regex

Star (asterisk) character

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 notation is universal and important.

This C# program uses the star character in a Regex pattern. Star means zero or more.

Example

Note

This example program demonstrates the usage of the star character in regular expressions. Because regular expression syntax is fairly universal, the star or asterisk character is useful in many implementations and also found in many language specifications.

The star indicates that zero or more matches of the preceding group must be found in the string for the result to be true. This means that you can use the star to specify optional characters. If you use the star on a group in parenthesis, the entire group is treated as optional and repeatable.

Program that uses star in regular expressions [C#]

using System;
using System.Text.RegularExpressions;

class Program
{
    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
    }
}

Output

True
True
True
False
True
True
False

Overview. The goal of this program is to demonstrate the usage of the star character in regular expressions and to call these regular expressions in the C# language. The star (asterisk) means that zero or more of the preceding character group should be matched. This means that if nothing matches the character group, the regular expression can still match based on other data.

Main method

Program execution. The program begins in the Main entry point and the result value from the IsMatch instance method is printed to the console when it is invoked on various Regex instances with various parameters. 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.

Compiler Principles

Kleene closures. The star is also called a Kleene closure in language theory. The parsing of the regular expression pattern allows you to use a parenthetical grouping and apply the star to that. The entire group must be repeated zero or more times.

Language theory

The star is also called the Kleene closure and it is part of several symbols that can describe languages. In compiler theory, regular expressions can specify regular languages. The regular expression notation can describe tokens by describing the lexemes that make up those tokens.

Dragon Book: Compilers

Summary

Regex type

We looked at the star character in regular expression syntax using enclosing C# programming language source text to test the patterns. The star or asterisk character means that zero or more instances should be matched in the string being tested. It can be used to specify an optional character or grouping, and is also called the Kleene closure in language theory.

Regex Type
.NET