Link Search Menu Expand Document

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:

  1. Find if a text contains a greater than symbol
  2. Find if a text contains a smaller than symbol
  3. Find if a text contains both greater than and smaller
  4. Find indexes of greater than and smaller than symbols
  5. Replace greater than and smaller than symbols
  6. 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 2 greater than (>) symbols

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 2 smaller than (<) symbols

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 if a Text Contains 3 smaller than (<) or greater than (>) symbols

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:

Finding Indexes of smaller than (<) or greater than (>) symbols within a string

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:

Replacing Greater than (>) and Smaller Than (<) Symbols with Other Characters

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:

Splitting a String using Greater than (>) and Smaller than (<) Symbols as Delimiters

Other useful articles:


Back to top

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