Link Search Menu Expand Document

How to Find Forward and Backward Slashes Using Regular Expression in C#

In this article, you will see how to find forward and backward slashes inside a string using regular expressions in C#.

The article explains how to:

  1. Find if a text contains forward and backward slashes
  2. Find indexes of forward and backward slashes
  3. Replace slashes with other characters inside a string
  4. Split a string using forward and backward slashes

Finding if a Text Contains a Forward Slash

Let’s first see how to find if a text contains a forward slash. To do so, create an object of the Regex class and pass the regular expression “/” to the C# Regex class constructor.

Next, call the “Matches()” method from the C# Regex class and pass it the string that you want to search for a forward slash.

The “Matches()” method returns a list of Match objects corresponding to the number of forward slashes found inside the text.

Here is an example:

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
   string textFile = "France/England are in Europe. You can visit by Car/Plane.";
   Regex myRegex = new Regex(@"/");
   var result = myRegex.Matches(textFile);
   if (result.Count > 0)
   Console.WriteLine("The sentence contains " + result.Count.ToString() + " forward slash(s)");
   Console.ReadLine();
 }
}
}

Output:

Finding if a Text Contains a Forward Slash

Finding if a Text Contains a Backward Slash

The process of finding a backward slash inside a string is similar to finding a forward slash except the regular expression that you need to pass to the Regex class constructor is “\”.

Double backslashes are passed instead of a single backslash because a backslash is a special character in Regex. Therefore, you need to escape it using another backslash.

Look at the following script for example.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
    string textFile = @"France\England are in Europe. You can visit by Car\Plane\Bus.";
    Regex myRegex = new Regex(@"\\");
    var result = myRegex.Matches(textFile);
    if (result.Count > 0)
    Console.WriteLine("The sentence contains " + result.Count.ToString() + " backward slash(s)");
    Console.ReadLine();
 }
}
}

Output:

Finding if a Text Contains a Backward Slash

Finding if a Text Contains Both Forward and Backward Slashes

You can also find both forward and backward slashes inside a string. To do so, you need to group the regular expressions to find forward and backward slashes. The resulting regular expression is “[/\]”.

The rest of the script remains the same.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
    string textFile = @"France\England are in Europe. You can visit by Car/Plane/Bus.";
    Regex myRegex = new Regex(@"[/\\]");
    var result = myRegex.Matches(textFile);
    if (result.Count > 0)
    Console.WriteLine("The sentence contains " + result.Count.ToString() + " slash(s)");
 }
}

Since, the input string in the above script contains a backward slash and two forward slashes, in the output you can see that a total of 3 matches are found. Output:

Finding if a Text Contains Both Forward and Backward Slashes

Finding Indexes of Forward and Backward Slashes within a String

The “Matches()” function returns a list of Match objects that contain information about the forward and backward slashes. You can use the Index attributes of the Match objects to access the indexes of forward and backward slashes.

The regular expression used will be the same as the last scripts i.e. “[/\\]”. Here is an example.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
    string textFile = @"France\England are in Europe. You can visit by Car/Plane/Bus.";
    Regex myRegex = new Regex(@"[/\\]");
    var result = myRegex.Matches(textFile);
    if (result.Count > 0)
      {
        foreach(Match m in result)
        {
        Console.WriteLine(m.Index);
        }
      }
 }
}
}

Output:

Finding Indexes of Forward and Backward Slashes within a String

Replacing Forward and Backward Slashes with Other Characters

You can use the “Replace()” method from the Regex class to replace forward and backward slashes within a text string. The regular expression used is “[/\]”.

Here is a sample script to replace forward and backward slashes with double asterisks.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
    string textFile = @"France\England are in Europe. You can visit by Car/Plane/Bus.";
    Regex myRegex = new Regex(@"[/\\]");
    var result = myRegex.Replace(textFile, "**");
    Console.WriteLine(result);
    Console.ReadLine();
 }
}
}

Output:

Replacing Forward and Backward Slashes with Other Characters

Splitting a String using Forward or Backward Slashes as Delimiters

Finally, you can use the “Split()” method from the C# Regex class to split a string using forward and backward slashes as delimiters.

The “Split()” method returns a list of split strings that you can iterate using a loop. Look at the following example script for reference.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
class Program
{
 static void Main(string[] args)
 {
    string textFile = @"France\England are in Europe. You can visit by Car/Plane/Bus.";
    Regex myRegex = new Regex(@"[/\\]");
    var result = myRegex.Split(textFile);
    foreach (string str in result)
    Console.WriteLine(str);
    Console.ReadLine();
   }
}
}

Since there is one backward slash and two forward slashes (a total of 3 slashes), the output below shows four substrings.

Output:

Splitting a String using Forward or Backward Slashes as Delimiters

Other useful articles:


Back to top

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