GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Regular Expressions
Selected Reading
C# - Regular ExpressionsA regular expression is a pattern that could be matched against an input text. The .Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs. Constructs for Defining Regular ExpressionsThere are various categories of characters, operators, and constructs that lets you to define regular expressions. Click the following links to find these constructs. The Regex ClassThe Regex class is used for representing a regular expression. It has the following commonly used methods −
For the complete list of methods and properties, please read the Microsoft documentation on C#. Use of Regular ExpressionsThe following are some uses of regular expressions:
Required Namespaces for Regular Expressions (RegEx)To work with Regex in C#, you need to include: using System; using System.Text.RegularExpressions; Basic Regex SyntaxHere are the syntaxes of some essential Regex elements:
String Pattern Matching Using RegExYou can check whether a string matches a specific pattern by using the ExampleThe following example matches words that start with 'S' − using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
static void Main(string[] args) {
string str = "A Thousand Splendid Suns";
Console.WriteLine("Matching words that start with 'S': ");
showMatch(str, @"\bS\S*");
Console.ReadKey();
}
}
}
The following example demonstrates matching words that start with 'S' in the string − Matching words that start with 'S': The Expression: \bS\S* Splendid Suns ExampleThe following example matches words that start with 'm' and end with 'e' − using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
static void Main(string[] args) {
string str = "make maze and manage to measure it";
Console.WriteLine("Matching words start with 'm' and ends with 'e':");
showMatch(str, @"\bm\S*e\b");
Console.ReadKey();
}
}
}
The following example demonstrates matching words that start with 'm' and end with 'e' − Matching words start with 'm' and ends with 'e': The Expression: \bm\S*e\b make maze manage measure String Replacement Using RegExYou can replace multiple spaces with a single space in a string using the ExampleThe following example replaces extra white spaces in a string with a single space − using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
class Program {
static void Main(string[] args) {
string input = "Hello World ";
string pattern = "\\s+";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
Console.ReadKey();
}
}
}
The following example demonstrates replacing multiple spaces with a single space − Original String: Hello World Replacement String: Hello World Email Validating Using RegExYou can validate an email address using a regular expression that checks for proper format (e.g., [email protected]). ExampleThe following example demonstrates how to validate an email address using regex − using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
class Program {
static void Main(string[] args) {
string email = "[email protected]";
string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
Regex rgx = new Regex(pattern);
bool isValid = rgx.IsMatch(email);
if (isValid) {
Console.WriteLine("The email address is valid.");
} else {
Console.WriteLine("The email address is invalid.");
}
Console.ReadKey();
}
}
}
The following example demonstrates how to validate an email address using regex − The email address is valid. Advertisements
| |||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |