C#第六周--关于正则表达式应用,delegates关键字

1.什么是正则表达式

在编写字符串的处理程序时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码。

    通常,我们在使用WINDOWS查找文件时,会使用通配符(*和?)。如果你想查找某个目录下的所有Word文档时,你就可以使用*.doc进行查找,在这里,*就被解释为任意字符串。和通配符类似,正则表达式也是用来进行文本匹配的工具,只不过比起通配符,它能更精确地描述你的需求——当然,代价就是更复杂

正则表达式中有一个关键的点就是元字符,所以我从网上查到了一个元字符的表格:

还有在正则表达式的基础应用中的限定符的应用

-----------------delegetes关键字-------------------------------------------------

这个关键字我理解为是一种功能函数实现的委托,类似C++/C中的函数指针,委托是类型安全的,而且支持加法,也就是功能的叠加

既可以实现单一委托:

using system;
public delegate void testdelegate(string message); //declare the delegate
class test
{
  public static void display(string message)
  {
    console.writeline("the string entered is : " + message);
  }
  static void main()
  {
    testdelegate t = new testdelegate(display); //instantiate the delegate
    console.writeline("please enter a string");
    string message = console.readline();
    t(message); //invoke the delegate
    console.readline();
  }
}

也可以实现多重委托

using system;
public delegate void testdelegate();
class test
{
  public static void display1()
  {
    console.writeline("this is the first method");
  }
  public static void display2()
  {
    console.writeline("this is the second method");
  }
  static void main()
  {
    testdelegate t1 = new testdelegate(display1);
    testdelegate t2 = new testdelegate(display2);
    t1 = t1 + t2; // make t1 a multi-cast delegate
    t1(); //invoke delegate
    console.readline();
  }
}
原文地址:https://www.cnblogs.com/tjullin-251249/p/4441060.html