How to Find the At (@) Sign Using Regular Expression in C#
This article shows how you can find the at the rate (@) symbol using regular expressions in C#. The @ symbol is normally used in email addresses. It is also often used as a replacement for the “at” proposition in the English language.
In this article, you will study how to:
- Find if a text contains an at symbol
- Find indexes of at signs within a text
- Replace at signs with other characters inside a string
- Split a string using an at sign
Finding if a Text Contains an At (@) Sign
To find if a text has an at symbol, you can use the “@” regex expression. You need to pass the regex expression inside the Regex class constructor.
Next, you can call the “Matches()” function to check for the existence of the at sign inside a string. The “Matches()” function returns a list of Match objects corresponding to the number of at signs found.
You can then print the number of at signs found using the “count” attribute of the list. Here is a script that shows how to check if a string contains an at sign in C#.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "John is @ the next square. Meet him @ that point"; Regex myRegex = new Regex(@"@"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " at (@) sign(s)"); Console.ReadLine(); } } }
Output:
Finding Indexes of At (@) Signs within a String
The “Matches()” function can again be used to find indexes of at signs within a string. The first step is to pass the “@” regex expression to the Regex class constructor. The rest of the steps are simple, you can access indexes of the at signs using the Indexes attribute of the Match object(s) returned by the “Matches()” function.
Here is an example of how you can find indexes of at signs within a string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "John is @ the next square. Meet him @ that point"; Regex myRegex = new Regex(@"@"); var result = myRegex.Matches(textFile); if (result.Count > 0) { foreach(Match m in result) { Console.WriteLine(m.Index); } } } } }
The output shows that the at sign is found at the 8th and 36th index of the input string.
Output:
Replacing At (@) Sign with Other Characters
To replace the at sign with other characters or strings using the regex expressions, you can use the “Replace()” method from the Regex class. But first you have to pass the regex expression “@” to the Regex class constructor. Next, you need to pass the input string as the first parameter to the “Replace()” function. The character/strings that you want to replace with the at sign in the input string is 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 = "John is @ the next square. Meet him @ that point"; Regex myRegex = new Regex(@"@"); var result = myRegex.Replace(textFile, "**"); Console.WriteLine(result); Console.ReadLine(); } } }
The output shows that at signs are replaced by asterisks.
Output:
Splitting a String using the At (@) Sign as a Delimiter
Finally, you can also use the at sign as a delimiter to split a string. The regex expression used for the Regex class constructor is “@”.
Next, you need to call the “Split()” method and pass it the input string as a parameter.
The “Split()” method returns a list of split strings.
The script below shows an example of splitting string via regular expressions. The strings split via the “Split()” function is iterated using a foreach loop. The split strings are then printed on the console.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "John is @ the next square. Meet him @ that point"; Regex myRegex = new Regex(@"@"); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }
Since the input string contains two at signs, you can see the three split strings in the output below:
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#