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
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
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:
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
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
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