Link Search Menu Expand Document

How to Find Hash Sign with Regex

In this article, you will see how to find hash signs within a C# string using regular expressions in C#. You will see how to get single as well as multiple hash signs. You will also see how to extract numbers with hash signs.

Finding a Single Hash Sign

To find a single hash sign, you can use the Match() method from the Regex class from the RegularExpressions module.

You need to pass the regex expression as the first parameter and the string that you want to search for the presence of the hash sign as the second parameter.

The regex expression you can use to search for the hash sign is simply “#”.

If a match is found, the Success attribute of the object returned by the Match() method will return true. You can then access the index at which the match is found using the Index attribute.

The following script searches for the presence of a single hash sign in a string and prints the index of the hash sign if  a match is found.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {

            string input = "Note the number 45565#  and 76557#.";

            string regex = @"#";

            var result = Regex.Match(input, regex);

            if (result.Success == true)
            {
                Console.WriteLine("Hash Sign found at index: " + result.Index.ToString());
            }

            Console.ReadLine();
        }
    }

}

The output below shows that a hash sign is found at the 21st index in our input string.

Output

Regular Expressions Find Hash Sign  

 

Finding  Multiple Hash Signs

To find multiple hash signs within a string, you can use the Matches() method. The regex expression remains the same i.e.  “#”.

The matches method returns a collection of Match objects where each object contains information such as the index of the matched hash sign.

The number of matched hash signs can be printed via the Count attribute of the collection returned by the Matches() method.

You can then iterate through the collection, and print the index value of each hash sign via the Index attribute of the corresponding Match object from the collection. Here is an example:

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Note the number 45565#  and 76557#.";

            string regex = @"#";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of hash signs found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Hash symbol " + i + " found at index: " + m.Index.ToString());
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

Output

Regular Expressions Finding Hash Sign  

 

Finding Hash Signs with Numbers

Oftentimes you want to find the numbers that occur together with a hash sign.

For instance, in the following script, the input string contains two hash signs with numbers. To fetch the numbers with hash signs, you can use the regex expression: “([0-9]*#)”. This regex expression consists of two parts: [0-9], and #. The * operator between these two parts makes sure that the two groups occur together.

The Matches() function can be then used to return all the hash signs with numbers.

using System; using System.Text.RegularExpressions;

namespace RegexCodes { class Program { static void Main(string[] args) { string input = “Note the number 45565#  and 76557#.”;

string regex = @”([0-9]*#)”;

var result = Regex.Matches(input, regex);

if (result.Count > 0) { Console.WriteLine(“Total count of amounts with hash sign found: “ + result.Count);

int i = 1; foreach (Match m in result) { Console.WriteLine(“Amount with hash sign “ + i + “ - “ + m.Value); i++; }

}

Console.ReadLine(); } } }

 

In the output of the script below, you can see the numbers along with the hash sign.

Output:

Regular Expressions How to Find Hash Sign  

 

Finding Numbers Prefixed or Postfixed with Hash Sign

Finally, you can also fetch numbers prefixed or postfixed with a hash sign. For instance, in the script below, numbers are prefixed with two hash signs.

To fetch both numbers and hash sign, you can use the regex: “([0-9]+)*(#)”. This regex expression searches for two groups of strings: digits from 0 to 9, and hash symbol.

The Matches() function in this case returns a list of Match objects that contain groups of numbers and hash signs. The number only can be accessed by passing 1 as the index value for the group as shown in the script below.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Note the number 45565#  and 76557#.";

            string regex = @"([0-9]+)*(#)";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of amounts with hash sign found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Amount with hash sign " + i + " - " + m.Groups[1].Value);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

 

In the output below, you can see the numbers only that occur with the hash signs.

Output

Reg Ex Find Hash Sign  

In the same way, you can use the regular expression “(#)*([0-9]+)” to fetch numbers that are postfixed with hash signs. In this case, the group at index 2 contains the numbers inside the Match object. Look at the script below for your reference.

using System;
using System.Text.RegularExpressions;

namespace RegexCodes
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Note the number #45565  and #76557.";

            string regex = @"(#)*([0-9]+)";

            var result = Regex.Matches(input, regex);

            if (result.Count > 0)
            {
                Console.WriteLine("Total count of amounts with hash sign found: " + result.Count);

                int i = 1;
                foreach (Match m in result)
                {
                    Console.WriteLine("Amount with hash sign " + i + " - " + m.Groups[2].Value);
                    i++;
                }

            }

            Console.ReadLine();
        }
    }
}

Output

Reg Ex Find Hash Sign

Other useful articles:


Back to top

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