Link Search Menu Expand Document

How to Find an Ampersand (&) Sign using Regular Expression in C#

In this article, you will see how to find an ampersand sign using regular expressions in c#. The ampersand sign is normally used as a shorthand notation for the “and” conjunction.

You will see how to:

  1. Find if a text contains an ampersand
  2. Find indexes of ampersand signs within a text
  3. Replace ampersand signs with other characters inside a string
  4. Split a string using an ampersand sign

Finding if a Text Contains an Ampersand

To find if a text contains an ampersand sign, you can use the “Matches()” method from the Regex class. To do so, you need to pass the “&” regex expression to the Regex class constructor.

Next, pass the string that you want to search for the ampersand sign to the “Matches()” method of the Regex class object. Here is an example.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
   class Program
   {
       static void Main(string[] args)
       {
           string textFile = "John & Marry loves playing tennis & football.";
           Regex myRegex = new Regex(@"&");
           var result = myRegex.Matches(textFile);

           if (result.Count > 0)
           Console.WriteLine("The sentence contains " + result.Count.ToString() + " ampersand(s)");

           Console.ReadLine();
       }
   }
}

 

In the output, you can see that 2 ampersand signs are found in the input string.

Output:

  Regular Expression How to Find Ampersand  

Finding Indexes of Ampersands within a String

You can also find the indexes of ampersand signs within a string. To do so, you can again use the “Matches()” function with the regex expression “&” inside the Regex class constructor.

The “Matches()” function returns Match class objects for each of the ampersand signs found inside the input string. The Match class object contains various information about the corresponding ampersand sign. You can use the Index attribute from the Match object to print the index of the ampersand signs. Look at the following script for reference.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
   class Program
   {
       static void Main(string[] args)
       {
           string textFile = "John & Marry loves playing tennis & football.";
           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 ampersands are found at indexes 5 and 34 of the input string.

Output:

  Regular Expression Find Ampersand  

Replacing Ampersands with Other Characters

You can also replace ampersand signs in a string with other characters. To do so, you can use the “Replace()” function from the Regex class. The regex expression used is “&”.

The replace function accepts two parameters. The input string and the string that you want to substitute with the ampersand signs inside the input string.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
   class Program
   {
       static void Main(string[] args)
       {
           string textFile = "John & Marry loves playing tennis & football.";
           Regex myRegex = new Regex(@"&");
           var result = myRegex.Replace(textFile, "**");
           Console.WriteLine(result);
           Console.ReadLine();
       }
   }
}

In the output, you can see that ampersand signs are replaced by asterisks.

Output:

  Reg Ex How to Find Ampersand  

Splitting a String using an Ampersand as a Delimiter

Splitting a string using an ampersand via regex is super easy. You need to pass the “&” sign as the regex expression for the Regex class constructor.

Next, you need to call the “Split()” method from the Regex class object and pass it to your input string that you want to split via the ampersand sign. The “Split()” method returns a list of multiple strings delimited via the ampersand sign.

The following script shows how to split a string using an ampersand sign as a delimiter.

A for loop is executed to iterate through the list of split strings returned by the “Split()” method.

using System.Text.RegularExpressions;
namespace RegexCodes
{
   class Program
   {
       static void Main(string[] args)
       {
           string textFile = "John & Marry loves playing tennis & football.";
           Regex myRegex = new Regex(@"&");
           var result = myRegex.Split(textFile);
           foreach (string str in result)
           Console.WriteLine(str);
           Console.ReadLine();
       }
   }
}

Since the input string in the above script contains two ampersand signs, it will be split into three substrings as shown in the output below.

Output:

  Reg Ex Find Ampersand

Other useful articles:


Back to top

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