The .NET Framework provides an object-oriented approach to regular expression pattern matching and replacement.
The Framework Class Library namespace System.Text.RegularExpressions is the home to all the .NET Framework objects associated with regular expressions. The central class for regular expression support is Regex, which provides methods and properties for working with regular expressions, the most important of which are shown.
Method or property | Explanation |
---|---|
Overloaded; creates an instance of Regex | |
Property that returns the options passed in to the constructor | |
Method that indicates whether a match is found in the input string | |
Searches an input string and returns a match for a regular expression | |
Searches an input string and returns all successful matches for a regular expression | |
Replace all occurrences of a pattern with a replacement string | |
Splits an input string into an array of substrings based on a regular expression |
Example Regular expressions
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace RegularExpressions
{
class Tester
{
public void Run()
{
string s1 =
"One,Two,Three Liberty Associates, Inc.";
Regex theRegex = new Regex(" |, |,");
StringBuilder sBuilder = new StringBuilder();
int id = 1;
foreach (string subString in theRegex.Split(s1))
{
sBuilder.AppendFormat(
"{0}: {1}\n", id++, subString);
}
Console.WriteLine("{0}", sBuilder);
}
[STAThread]
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}
Output:
1: One
2: Two
3: Three
4: Liberty
5: Associates
6: Inc.
This can be a bit confusing. In the context of a C# program, which is the regular expression: the text passed in to the constructor or the Regex object itself? It is true that the text string passed to the constructor is a regular expression in the traditional sense of the term. From a C# (i.e., object-oriented) point of view, however, the argument to the constructor is just a string of characters; it is the object called theRegex that is the regular expression object.
RELATED POST
VALIDATION TESTING
SYSTEM TESTING
DEBUGGING AND TESTING
DEFECT AMPLIFICATION AND REMOVAL
ITERATIVE SPIRAL MODEL
STANDARD WATER MODEL
CONFIGURATION MANAGEMENT
CONTROLLED TESTING ENVIRONMENT
RISK ANALYSIS PART ONE
RISK ANALYSIS PART TWO
BACK GROUND ISSUES
SOFTWARE REVIEWS PART ONE
SOFTWARE REVIEWS PART TWO
SOFTWARE RELIABILITY
SAFETY ASPECTS
MISTAKE PROOFING
SCRIPT ENVIRONMENT
V MODEL IN TESTING
No comments:
Post a Comment