How to Find the Greater Than (>) and Smaller Than (<) Signs Using Regular Expression in C#
In this article, you will see how to find greater than (>) and smaller than (<) symbols inside a string using regular expressions in C#.
The article explains how to:
- Find if a text contains a greater than symbol
- Find if a text contains a smaller than symbol
- Find if a text contains both greater than and smaller
- Find indexes of greater than and smaller than symbols
- Replace greater than and smaller than symbols
- Split a string using greater than and smaller than symbols
Finding if a Text Contains a Greater Than (>) Symbol
To find if a text contains a greater than symbol, you can use the Matches() function from the Regex class.
To match a greater than symbol, you can use the “>” regex expression. The regex expression must be passed to the Regex class constructor, which returns a regex object.
The Regex class object can then be used to call the Matches() method, which returns a list of Match objects. Each Match object corresponds to a match found inside the text string.
You can find the total number of greater than symbols found using the count property of the list returned by the Matches() method.
Here is an example:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "USA's population > England's Population. Inch > cm"; Regex myRegex = new Regex(@">"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " greater than symbol(s)"); Console.ReadLine(); } } }
Output:
Finding if a Text Contains a Smaller Than (<) Symbol
The process of finding a smaller than symbol is similar to finding the greater than a symbol. The regex expression used for the Match() function, in this case, is “<. “
Look at the following script, for example.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "England's population < China's Population. cm < inch"; Regex myRegex = new Regex(@"<"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " smaller than symbol(s)"); Console.ReadLine(); } } }
Output:
Finding if a Text Contains Both Greater than and Smaller Than Symbols
You can also use the Matches() function to find if a text contains both the greater than and smaller than symbols.
In this case, you need to group the individual regular expressions used to find the greater than symbol and the smaller than symbol.
You can use square brackets to group multiple regex symbols. To find both the greater than and smaller than symbols, the grouped regular expression looks like this ”[<>]”.
Here is an example that finds both the greater than and smaller than symbols within a text string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "England's population < China's Population. inch > cm. Mile > KM"; Regex myRegex = new Regex(@"[<>]"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " smaller than or greater than symbol(s)"); Console.ReadLine(); } } }
Output:
Finding Indexes of Greater than and Smaller than Symbols within a String
You can also find indexes of greater than and smaller than symbols within a text string. To do so, you can use the Matches() function again ”[<>]”.
As discussed earlier, the Matches() method returns a list of Match() objects corresponding to each match found inside a text string. The Index attribute of each Match object can be used to print the index of a greater than or a smaller than symbol.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "England's population < China's Population. inch > cm. Mile > KM"; Regex myRegex = new Regex(@"[<>]"); var result = myRegex.Matches(textFile); if (result.Count > 0) { foreach (Match m in result) { Console.WriteLine(m.Index); } } Console.ReadLine(); } } }
Output:
Replacing Greater than and Smaller Than Symbols with Other Characters
You can also replace greater than or smaller than symbols with other characters. You can use the Replace() function of the regex class. The string where you replace the characters is passed as the first parameter to the Replace() function. The characters that replace the greater than or smaller than symbols are passed as the second parameter.
Here is an example:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "England's population < China's Population. inch > cm. Mile > KM"; Regex myRegex = new Regex(@"[<>]"); var result = myRegex.Replace(textFile, "**"); Console.WriteLine(result); Console.ReadLine(); } } }
Output:
Splitting a String using Greater than and Smaller than Symbols as Delimiters
Finally, you can also split a string using greater than or smaller than symbols. You need to pass the input string to the Split() method of the Regex class in this case. Here is an example:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "England's population < China's Population. inch > cm. Mile > KM"; Regex myRegex = new Regex(@"[<>]"); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }
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
- How to find the Tilde (~) sign using Regular Expression in C#
- How to Find the Greater Than (>) and Smaller Than (<) signs using Regular Expression in C#