How to find the Tilde (~) sign using Regular Expression in C#
In this article, you will see how to find the tilde “~” sign using regular expressions in C#.
The tilde sign is often used before a number to signify approximation. For instance, when you say ~50, it means about 50.
This article shows how to:
- Find if a text contains the tilde sign
- Find indexes of tilde signs within a text
- Replace tilde signs with other characters inside a string
- Split a string using a tilde sign
Finding if a Text Contains a Tilde Sign
You can use the “Matches()” function from the Regex class to see if a string contains any character e.g. tilde, comma, etc. To find the tilde sign, you need to pass the “~” regex expression to the Regex class constructor.
Next, the string that you want to search for the presence of the tilde sign is passed to the “Matches()” method.
If the tilde sign is found inside a string, the “Matches()” method returns a list of Match objects which contain information about the tilde signs found inside a string.
The count attribute of the list containing Match objects can be used to print the number of tilde signs found inside a string.
Here is how to find the tilde sign within a string:
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Paris ~ is the ~ capital of ~ France."; Regex myRegex = new Regex(@"~"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains " + result.Count.ToString() + " tilde sign(s)"); Console.ReadLine(); } } }
The output shows that the input sentence contains three tilde signs.
Output:
Finding Indexes of Tilde Signs within a String
The “Matches()” function can also be used to find indexes of tilde signs. The regex expression used will be the same i.e. “~”.
The list of Match objects returned by the “Matches()” method contains various information about the tilde sign found in the input string.
You can iterate through the list of Match objects and print the indexes of the tilde signs using the Index attribute.
The following script shows how to find indexes of tilde signs within a string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Paris ~ is the ~ capital of ~ France."; Regex myRegex = new Regex(@"~"); var result = myRegex.Matches(textFile); if (result.Count > 0) { foreach(Match m in result) { Console.WriteLine(m.Index); } } } } }
Output:
Replacing Tilde with Other Characters
The “Replace()” function from the Regex class is used to replace tilde signs in an input string, with other characters/strings.
First you have to pass “~” as the Regex parameter. Next, you have to pass the input string as the first parameter to the “Replace()” function of the Regex class.
Finally, pass the character/string that you want your tilde signs to be replaced with as the second parameter to the “Replace()” method.
Here is an example of how to replace the “tilde” signs within a string.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Paris ~ is the ~ capital of ~ France."; Regex myRegex = new Regex(@"~"); var result = myRegex.Replace(textFile, "**"); Console.WriteLine(result); Console.ReadLine(); } } }
Output:
Splitting a String using a Tilde Sign as a Delimiter
To split a string using the tilde sign as a delimiter, you can use the “Split()” method from the Regex class. The regex expression remains the same i.e “~”.
The “split()” method returns a list of splitted strings which you can iterate via a for loop.
The following script shows an example of how to split a string using the “tilde” sign as a delimiter.
using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Paris ~ is the ~ capital of ~ France."; Regex myRegex = new Regex(@"~"); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }
Since there are three tilde signs in the input sting in the above script, you will see that the input string will be split into four parts as shown 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#
- How to Find the Carrot (^) Sign Using Regular Expression in C#
- How to Find the Equals (=) Sign using Regular Expression in C Sharp
- How to find the Tilde (~) sign using Regular Expression in C#