How to Find the Equals (=) Sign using Regular Expression in C Sharp
This article explains how you can find the equals sign within a text string using the regular expressions in C#.
The following concepts are explained in this article:
- Finding if a text contains an equals Sign
- Finding indexes of equal signs within a string
- Replace equals signs with other characters inside a string
- Split a string using an equal sign
Finding if a Text Contains an Equals (=) Sign
To find if a text string contains an equals sign, create an object of the Regex class. In the class constructor, pass the regex expression “=”.
Next, pass the input string to the “Matches()” function of the Regex class. The “Matches()” function returns a list of Match objects. The list length is equal to the number of equal signs found.
The following script shows how to print the number of equals signs found inside a text string:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This text = contains = equals to sign"; Regex myRegex = new Regex(@"="); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " equals (=) sign(s)"); Console.ReadLine(); } }
The output below shows that the text contains 3 equals signs.
Output
Finding Indexes of Equals (=) Signs within a String
You can also find indexes of equals signs within a string using the “Matches()” function. The regex expression for the Regex class constructor will be “=”.
The “Matches()” function returns a list that contains Match objects for all the equals signs found within a string. The Index attribute of these Match objects can be used to find indexes of equals signs in Python.
The script below shows how you can find indexes of equals signs within a string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This text = contains = equals to sign"; Regex myRegex = new Regex(@"="); var result = myRegex.Matches(textFile); if (result.Count > 0) { foreach(Match m in result) { Console.WriteLine(m.Index); } } } } }
The output below shows that equals signs are found at indexes 10 and 21 of the input string.
Output
Replacing Equals (=) Sign with Other Characters
You can use the “Replace()” method from the Regex class to replace equals signs in a string with other characters/strings. The regex expression remains the same.
You need to pass two parameters to the “Replace()” method. The first one is the input string that contains equals signs. The second parameter is the character/string that replaces the equals sign within the input string.
As an example, the following script replaces the two equals signs in the input strings with double asterisks.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This text = contains = equals to sign"; Regex myRegex = new Regex(@"="); var result = myRegex.Replace(textFile, "**"); Console.WriteLine(result); Console.ReadLine(); } } }
In the output below, you can see that equals signs are replaced by double asterisks.
Output
Splitting a String using the Equals (=) Sign as a Delimiter
Finally, you can also use the equals sign as a delimiter to split strings. You can use the “Split()” function of the Regex class. The regex expression will remain the same i.e. “=”.
The “Split()” function will return a list of strings split from the places where the equals sign is present in the string. You can then iterate through the list and print the substrings.
The script below shows how to split a string using the equals sign as a delimiter in regular expressions.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This text = contains = equals to sign"; Regex myRegex = new Regex(@"="); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }
Since, the input string had two equals signs, in the output below you can see that it is split into three parts.
Output
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
- How to Split Text Using Regex
- How to Find HTML Tags Using Regex
- How to Validate Email Address via Regex in C#
- How to Extract Amount with Currency Symbols using Regex in C#
- How to Find Brackets with Regex
- How to Find Hash Sign with Regex
- How to Find Percentage Symbol with Regex
- How to Find Word Frequencies using Regex
- Find an Ampersand (&) Sign using Regular Expression in C#
- How to Find the At (@) Sign Using Regular Expression in C#
- How to Find the Carrot (^) Sign Using Regular Expression in C#
- How to Find the Equals (=) Sign using Regular Expression in C Sharp