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:
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.
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:
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:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C