Link Search Menu Expand Document

How to Find a Semicolon using Regular Expression in C Sharp

Finding special characters such as semicolons, commas, periods, etc. can be handy for text preprocessing tasks which is the first step in natural language processing.

This article shows how you can find a semicolon in a text string using C# regular expressions (regex). You will also study how to find indexes of semicolons in a string, how to replace semicolons with other characters, and how to split a string using a semicolon as a delimiter.

Finding if a Text String Contains a Semicolon

In the following example, you will see if a text string contains one or more semicolons. To do so, you can use the Matches() function from the Regex class of the RegularExpressions module in C#.

As a first step, you have to pass the regex expression “;” to the constructor of the Regex class. Next, you need to call the Matches() method and pass it to your input string. If a text string contains one more semicolons, the Count attribute of the object returned by the Matches() method will contain a value greater than 0.

Here is an example:

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Is there; a semicolon; in this, sentence?"; Regex myRegex = new Regex(@";"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " semicolons(s)"); Console.ReadLine(); } } }

In the script above, the textFile contains a string that contains two semicolons. The Matches() method will return an object whose Count attribute will contain 2. Thus, in the console output, you will see the following message.

Reg Ex C Sharp Semicolon

Finding Indexes of Commas within a String

You can also find the index i.e. position of a semicolon within a string using C# regular expressions. To do so, you can use the Matches() function again. The Matches() function returns a collection of Match objects. Each match object contains information about the matched object including its index which can be printed using the Index attribute. Look at the following example.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This string contains a semicolon; and two more; and here;"; Regex myRegex = new Regex(@";"); var result = myRegex.Matches(textFile); if (result.Count > 0) { foreach (Match m in result) { Console.WriteLine(m.Index); } } Console.ReadLine(); } } }

In the above script, the textField variable stores a string which contains three semicolons at different positions within a string. The Matches() function returns a collection of Match objects where a particular Match object stores information about one semicolon.

A foreach loop iterates through each Match object in the collection and the value of the Index attribute of the Match object is printed on the console. In the output below, you can see indexes for the three semicolons in the textField string.

Reg Ex C Sharp Find a Semicolon

Replacing a Semicolon with Other Characters

You can replace a semicolon with other characters in a string using the Replace() function from the Regex library. You need to pass the regex expression “,” to the Regex class constructor. Next, you can call the Replace() function from the Regex class object and pass it as a first parameter, the string in which you want to make replacements. The second parameter is the string that you want to replace the semicolon with.

For example, in the following script, a semicolon is replaced by a dash character using the Regex Replace() method.

using System; using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This string contains a semicolon; and two more; and here;"; Regex myRegex = new Regex(";"); var result = myRegex.Replace(textFile, "-"); Console.WriteLine(result); Console.ReadLine(); } } }

The output below shows that the updated string now contains dashes instead of semicolons.

Reg Ex C Sharp How to Find Semicolon  

Splitting a String using Semicolon as a Delimiter

Splitting a string with C# regular expressions is super easy. You need to pass the regex expression to the constructor of the Regex class while initializing the Regex class object. The regex expression used to split a string using a semicolon is “;”.

Next, you need to call the Split() method using the Regex class object and pass it to the string that you want to split.

For instance, the following script splits a string using a semicolon as a delimiter.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This string contains a semicolon; and two more; and here;"; Regex myRegex = new Regex(";"); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }

Since there are three semicolons in the textField string, and one of the semicolons is at the end of the string, you will see the following substrings in the output.

Regular Expressions Semicolon

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy