TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Star: Regex

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

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 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf