Link Search Menu Expand Document

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:

  1. Find if a text contains an at symbol
  2. Find indexes of at signs within a text
  3. Replace at signs with other characters inside a string
  4. 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 if a Text Contains 2 at (@) Sings

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:

Finding Indexes of At (@) Signs within a String

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:

Replacing At (@) Sign with Other Characters

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:

How to Use Regex in C

Other useful articles:


Back to top

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