C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
Example. This example program uses 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.
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 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("TheDeveloperBlog")); // True // // Zero or more s letters are allowed. // regex = new Regex("deves*"); Console.WriteLine(regex.IsMatch("perl")); // True Console.WriteLine(regex.IsMatch("devessss")); // 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
We use 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.
Note: This means if nothing matches the character group, the regular expression can still match based on other data.
The program begins in the Main method. 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.
Closures. A star is also called a Kleene closure. 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.
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.
Summary. We looked at the star character in regular expression syntax using enclosing C# code to test the patterns. The star or asterisk character means that zero or more instances should be matched in the string being tested.