12 December 2007

Regular Expression : How to use in C#

Regular expression is a very useful tool for string matching. Through regular expression we can apply a pattern on an input string and return a list of matches within the text. Here I'll show how to use this important tool in C#. Constructing pattern is a very important thing. But I’ll not concentrate on that, rather use a simple pattern to show the way of implementation in C#.

► System.Text.RegularExpressions namespace is required.

The above namespace contains a class Regex. The most common constructor is public Regex(string pattern)

Regex contains a function named Match. Two important versions are

1. public static Match Match(string pattern, string inputString)

2. public Match Match(string inputString)

we can use 1st version without creating any instance of Regex, as it is a static function. To use the 2nd version, we must first create an instance of Regex.

Match function returns the result of matching the pattern within the inputString through a Match object.

Through Success attribute of Match class, we can know whether pattern successfully matches or not.

If successful, then we can know the index of inputString where the match starts at from the attribute Index of Match. Another attribute Value gives the matched part.

Sample program:

using System;
using System.Text;
using System.Text.RegularExpressions;
namespace RegeularExpressionTest
{
class Program
{
static void
Main(string[] args)
{
string inputStr = "i am a software engineer, not a mechanical engineer";
string pattern = "engineer";
Regex r = new Regex(pattern);
Match m = r.Match(inputStr);
printResult(m);
}
static void printResult(Match m)
{
if (m.Success)
Console.WriteLine("Pattern matched:\tIndex:{0}\tValue:\'{1}\'", m.Index, m.Value);
else
Console.WriteLine("Pattern did not match.");
}
}
}

/*
output:
-------
Pattern matched: Index:16 Value:'engineer'
*/

After getting one match, we can further search for more matches through the function nextMatch.

Sample program:

string inputStr = "i am a software engineer, not a mechanical engineer";
string pattern = "engineer";
Regex r = new Regex(pattern);
Match m = r.Match(inputStr);
printResult(m);
m = m.NextMatch();
printResult(m);
m = m.NextMatch();
printResult(m);

/*
output:
-------
Pattern matched: Index:16 Value:'engineer'
Pattern matched: Index:43 Value:'engineer'
Pattern did not match.
*/

The function Matches can be used to get all successful matches.

Sample program:

string inputStr = "i am a software engineer, not a mechanical engineer";
string pattern = "engineer";
Regex r = new Regex(pattern);
MatchCollection mc = r.Matches(inputStr);
foreach (Match m in mc)
printResult(m);

/*
output:
-------
Pattern matched: Index:16 Value:'engineer'
Pattern matched: Index:43 Value:'engineer'
*/

Regex can be used for matching from the last i.e. right to left. Here RegexOption has to be used. RegexOption can also used for case-insensitive match.

Regex can also be used for string replacing.

Sample program:

string inputStr = "i am a software EngInEer, not a mechanical enGiNeeR";
string pattern = "engineer";
Regex r = new Regex(pattern, RegexOptions.RightToLeft | RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(inputStr);
foreach (Match m in mc)
printResult(m);

string replacingStr = "ENGR";
string replacedStr = r.Replace(inputStr, replacingStr);
Console.WriteLine(replacedStr);
/*
output:
-------
Pattern matched: Index:43 Value:'enGiNeeR'
Pattern matched: Index:16 Value:'EngInEer'
i am a software ENGR, not a mechanical ENGR
*/

0 Comments:

 

© 2007 t!ps n tr!cks: Regular Expression : How to use in C#



Template unik dari rohman


---[[ Skip to top ]]---