using System;
using System.Text.RegularExpressions;
namespace ConsoleRegex
{
class Program
{
static void Main(string[] args)
{
//pattern is a string which alphanumeric characters/symbols have meaning beyond the normal meaning. So, also called metacharacter. meta = beyond. For example, [ ] - etc.
string strPattern = "[0-9]"; // search a digit from the range of digits between 0 and 9
// Regex is an immutable class
// regex is a object which is constructed by passing pattern
// Regex class constructor is parameterized. Overloaded constructors are also to provide case //insensitive match. RegexOptions.IgnoreCase or RegexOptions.Singleline,
//RegexOptions.Multiline or RegexOptions.IgnorePatternWhitespace:
Regex regex = new Regex(strPattern);
Console.WriteLine(regex.ToString()); // get the pattern or regular expression
string strInput = "Number 13 is lucky or not?";
//Match method of regex returns an object of Match class
Match match = regex.Match(strInput);
Console.WriteLine(match.Success);
Console.WriteLine(match.Value);
Console.WriteLine(match.Index);//1 is at 7th position, 0-based index
Console.WriteLine(regex.IsMatch(strInput));
//Matches() method returns a collection of Match type objects called MatchCollection class
foreach (Match m in regex.Matches(strInput))
{
Console.WriteLine(m.Value);
}
foreach (var item in regex.Split(strInput))
{
Console.WriteLine("... "+item);
}
Console.ReadKey();
}
}
}
OUTPUT
[0-9]
True
1
7
True
1
3
... Number
...
... is lucky or not?
Read More at Microsoft site Link
No comments:
Post a Comment