Link Search Menu Expand Document

How to Find a Dot using Regular Expression in C

In this article, you will study how to find a dot in a string using regular expressions in C#. Text sentences in documents are often separated using commas, semicolons, and dots. Finding dots within a string can help you identify the number of sentences in a string. You can also split texts using a dot. Finally, you may need to replace the dot with other characters. In this article, you will perform all of these tasks. So let’s begin without any ado.

Finding if a Text String Contains a Dot

The Matches() function from the Regex of the RegularExpressions module can be used to find if a string contains one or more dots.

The first step is to initialize an object of the Regex class and pass the regex expression “\.” to the Regex class constructor.

Next, you need to call the Matches() function and pass it to the input string in which you want to look for dots. The Matches() function returns a collection of Match object. You can check the Count attribute of the collection. If the value of the Count attribute is greater than one, you can say that the string contains one or more dots.

Here is an example.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Welcome to regex. You are going to love it."; Regex myRegex = new Regex(@"\."); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " dots(s)"); Console.ReadLine(); } } }

In the script above, the input string textFile contains two dots. Therefore, you should see the following output:

Regular Expressions C Sharp Dot  

Finding Indexes of Dots within a String

The Matches() function also allows you to find indexes of all the dots within a string. An index basically refers to the starting position of a string or character within a string. A character at the start of a string will have an index of 0.

You can find indexes of all the dots in your string using the Index attributes from the collection of Match objects returned by the Matches() function. The regex expression passed to the Regex class constructor will remain the same i.e. “\.”.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Welcome to regex. You are going to love it."; 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 script above, the input string textFile contains two dots. In the output, you can see the Index positions of these two dots.

Reg Ex C Sharp Dot

Replacing a Dot with Other Characters

You can use the Replace() function from the Regex class to replace a dot within a string with any other character or a string. The process is familiar to finding dots and their indexes.

You need to pass the regex expression “\.” to the Regex class constructor. Next, while calling the Replace() method, you need to pass the input string containing the dots, as first the first parameter value. The second parameter value for the Replace() method will be the string or character that will replace the dots in the input string.

In the following script, the dots in the input string are replaced by dashes.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Welcome to regex. You are going to love it."; Regex myRegex = new Regex(@"\."); var result = myRegex.Replace(textFile, "-"); Console.WriteLine(result); Console.ReadLine(); } } }

Here is the output of the script above:

Dot Regular Expressions C Sharp

Splitting a String using Dots as  Delimiters

The Split() function from the Regex class can split a string using any character as a delimiter. To split a string using the dot operator, you have to pass the regex expression “.” as a parameter value to the regex class constructor. Next, you need to call the Split() method and pass it to the input string.

Here is an example:

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Welcome to regex. You are going to love it. It is amazing."; Regex myRegex = new Regex(@"\."); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }

In the script above the textFile string contains three dots, where one of the dots is at the end of the string. Hence, in the output, you will see three substrings as shown below:

Dot Reg Ex C Sharp Dot

Other useful articles:


Back to top

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