In addition, the regexp classes implement some additional functionality, such as named capture groups, right- to-left pattern matching, & expression compilation.
Regular expressions have been used in various programming languages & tools for plenty of years. The .NET Base Class Libraries include a namespace & a set of classes for utilizing the power of regular expressions. They are designed to be compatible with Perl 5 regular expressions whenever possible.
In this editorial, I'll provide a fast overview of the classes & methods of the System.Text.RegularExpression assembly, some examples of matching & replacing strings, a more detailed walk-through of a grouping structure, & eventually, a set of cookbook expressions for use in your own applications.
The RegularExpression Assembly
The classes listed in the Assembly regexp System.Text.RegularExpressions.dll, and you must reference the assembly at compile time in order to build your application. For example: csc / r: assembly foo.cs foo.exe System.Text.RegularExpressions.dll draw, with a reference to System.Text.RegularExpressions Assembly.
Program that uses Regex.Match (C#)
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// First we see the input string.
string input = "/content/alternate-1.aspx";
// Here we call Regex.Match.
Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
0 comments:
Post a Comment