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:
- Find if a text contains forward and backward slashes
- Find indexes of forward and backward slashes
- Replace slashes with other characters inside a string
- 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 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 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 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:
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:
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:
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 Forward and Backward Slashes using Regular Expression in C#