| 弹出窗口杀手(上) |
| C#编程规范 |
| 利用C#重启远程计算机(2) |
| 局域网中根据ip地址反查主机的名称(c#) |
| 昨天在网上找到了一个控件组DotNetBar62 |
| VisualC#事件与接口编程实例 |
| 从原始到优雅:C# 验证的四个阶段 |
| .net的reflection(2) |
| 利用C#重启远程计算机(1) |
| C#程序控制开始菜单的弹出 |
| String Text=@"I can not find my position in Beijing"; |
| String Pattern = "ion"; MatchCollection Matches = Regex.Matches(Text,Pattern,RegexOptions); foreach(Match NextMatch in Matches) { Console.WriteLine(NextMatch.Index); } |
| String Pattern = @"\bn"; MatchCollection Matches = Regex.Matches(Text,Pattern,RegexOptions.IgnoreCase| RegexOptions.ExplicitCapture); |
| String Pattern = @"ion\b"; |
| String Pattern = @"\bn\S*ion\b"; |
| 特定字符或转义序列 | 含义 | 样例 | 匹配的样例 |
| ^ | 输入文本的开头 | ^B | B,但只能是文本中的第一个字符 |
| $ | 输入文本的结尾 | X$ | X,但只能是文本中的最后一个字符 |
| . | 除了换行字符(\n)以外的所有单个字符 | i.ation | isation、ization |
| * | 可以重复0次或多次的前导字符 | ra*t | rat、raat等 |
| + | 可以重复1次或多次的前导字符 | ra+t | rt、rat、raat等 |
| ? | 可以重复0次或1次的前导字符 | ra?t | 只有rt和rat匹配 |
| \s | 任何空白字符 | \sa | [space]a,\ta,\na(\t和\n与C#的\t和\n含义相同) |
| \S | 任何不是空白的字符 | \SF | aF,rF,cF,但不能是\tf |
| \b | 字边界 | ion\b | 以ion结尾的任何字 |
| \B | 不是字边界的位置 | \BX\B | 字中间的任何X |
| static void WriteMatches(string text, MatchCollection matches) { Console.WriteLine("Original text was: \n\n" + text + "\n"); Console.WriteLine("No. of matches: " + matches.Count); foreach (Match nextMatch in matches) { int Index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (Index < 5) ? Index : 5; int fromEnd = text.Length - Index - result.Length; int charsAfter = (fromEnd < 5) ? fromEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; Console.WriteLine("Index: {0}, \tString: {1}, \t{2}",Index, result, text.Substring(Index - charsBefore, charsToDisplay)); } } |
| RegexOption 成员 | 内联字符 | 说明 |
| None | 无 | 指定不设置任何选项。 |
| IgnoreCase | i | 指定不区分大小写的匹配。 |
| Multiline | m | 指定多行模式。更改 ^ 和 $ 的含义,以使它们分别与任何行的开头和结尾匹配,而不只是与整个字符串的开头和结尾匹配。 |
| ExplicitCapture | n | 指定唯一有效的捕获是显式命名或编号的 (?<name>...) 形式的组。这允许圆括号充当非捕获组,从而避免了由 (?:...) 导致的语法上的笨拙。 |
| Compiled | 无 | 指定正则表达式将被编译为程序集。生成该正则表达式的 Microsoft 中间语言 (MSIL) 代码;以较长的启动时间为代价,得到更快的执行速度。 |
| Singleline | s | 指定单行模式。更改句点字符 (.) 的含义,以使它与每个字符(而不是除 \n 外的所有字符)匹配。 |
| IgnorePatternWhitespace | x | 指定从模式中排除非转义空白并启用数字符号 (#) 后面的注释。请注意,空白永远不会从字符类中消除。 |
| RightToLeft | 无 | 指定搜索是从右向左而不是从左向右进行的。具有此选项的正则表达式将移动到起始位置的左边而不是右边。(因此,起始位置应指定为字符串的结尾而不是开头。)为了避免构造具有无限循环的正则表达式的可能性,此选项不能在中流指定。但是,(?<) 回顾后发构造提供了可用作子表达式的类似替代物。 |
| ECMAScript | 无 | 指定已为表达式启用了符合 ECMAScript 的行为。此选项仅可与 IgnoreCase 和 Multiline 标志一起使用。将 ECMAScript 同任何其他标志一起使用将导致异常。 |
| static void Find_po() { string text = @" I can not find my position in Beijing "; string pattern = @"\bpo\S*ion\b"; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); WriteMatches(text, matches); } |
| using System; using System.Text.RegularExpressions; |
| String Extension(String url) { Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", RegexOptions.Compiled); return r.Match(url).Result("${proto}${port}"); } |