1、把正则表达式编译为CIL:RegexOptions.Compiled 作为第二个参数传给Regex()构造函数。
Regex regexObj = new Regex("regex pattern",RegexOptions.Compiled);
参数:
宽松排列: RegexOptioins.IgnorePatternWhitespace
不区分大小写:RegexOptioins.IgnoreCase
点号匹配换行符:RegexOptioins.Singleline
脱字符和美元符号匹配换行处: RegexOptioins.Multiline
2、从windows路径中抽取盘符:在windows路径中只能以冒号来作为分隔符
^([a-z]):
3、逐字字符串:以@"开头,引用双引号需要用两个引号,反斜杠不需要转义
@"who are you ""I am .."""
4、获取匹配文本
string resultstring = Regex.Match(source,@"\d+").Value;
重复使用一个正则表达式,就要构造一个Regex对象
Regex reg = new Regex(@"\d+");
string resultstring = reg.Match(source).Value;
5、获取文本的一部分,如果只是一次性的快速检查
string resultstring = Regex.Match(source,"http://([a-z0-9.-]+)").Groups[1].Value;
如果重复使用同一个正则式,
Regex reg = new Regex("http://([a-z0-9.-]+)");
string resultstring = reg.Match(source).Groups[1].Value;