正则表达式-定位点
匹配开头的结尾的,主要差别在使用了RegexOptions.Multiline多行模式上,看下面两个示例:
string pattern = @"^abc"; string str = "zzz\nabc"; Regex regex = new Regex(pattern, RegexOptions.Multiline); bool b = regex.IsMatch(str); Console.WriteLine(b); //True string pattern = @"\Aabc"; string str = "zzz\nabc"; Regex regex = new Regex(pattern, RegexOptions.Multiline); bool b = regex.IsMatch(str); Console.WriteLine(b); //False
再看MSDN的叙述:
^
指定匹配必须出现在字符串的开头或行的开头。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。
$
指定匹配必须出现在以下位置:字符串结尾、字符串结尾的 \n 之前或行的结尾。有关更多信息,请参阅正则表达式选项中的 Multiline 选项。
\A
指定匹配必须出现在字符串的开头(忽略 Multiline 选项)。
\Z
指定匹配必须出现在字符串的结尾或字符串结尾的 \n 之前(忽略 Multiline 选项)。
\z
指定匹配必须出现在字符串的结尾(忽略 Multiline 选项)。
\G 定位标记指定匹配必须出现在上一个匹配结束的地方。 通过 Regex.Matches 或 Match.NextMatch 方法使用此定位点时,它确保所有匹配项是连续的。
下面的示例使用正则表达式从以逗号分隔的字符串提取啮齿类的名称。
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string input = "capybara,squirrel,chipmunk,porcupine,gopher," + "beaver,groundhog,hamster,guinea pig,gerbil," + "chinchilla,prairie dog,mouse,rat"; string pattern = @"\G(\w+\s?\w*),?"; Match match = Regex.Match(input, pattern); while (match.Success) { Console.WriteLine(match.Groups[1].Value); match = match.NextMatch(); } } } // The example displays the following output: // capybara // squirrel // chipmunk // porcupine // gopher // beaver // groundhog // hamster // guinea pig // gerbil // chinchilla // prairie dog // mouse // rat
\b 定位标记指定匹配必须出现在单词字符(\w 语言元素)和非单词字符(\W 语言元素)之间的边界上。 单词字符包含字母数字字符和下划线;非单词字符是非字母数字或下划线的任何字符。(有关更多信息,请参见正则表达式中的字符类。)匹配也可能出现在字符串的开头或结尾的单词边界。
\b 定位标记经常用于确保子表达式匹配整个单词而不只是匹配单词的开头或结尾。 下例中的正则表达式 \bare\w*\b 演示此用法。 它匹配以子字符串“are”开头的任意单词。 示例的输出还演示 \b 同时匹配输入字符串的开头和结尾。
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string input = "area bare arena mare"; string pattern = @"\bare\w*\b"; Console.WriteLine("Words that begin with 'are':"); foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine("'{0}' found at position {1}", match.Value, match.Index); } } // The example displays the following output: // Words that begin with 'are': // 'area' found at position 0 // 'arena' found at position 10
\B 定位标记指定匹配不得出现在单词边界。 它是与 \b 定位点相反的定位点。
下面的示例使用 \B 定位点找到单词中子字符串“qu”的匹配项。 正则表达式模式 \Bqu\w+ 匹配以“qu”开头的子字符串,该字符串不会开始单词并延续到字符串的结尾。
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string input = "equity queen equip acquaint quiet"; string pattern = @"\Bqu\w+"; foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine("'{0}' found at position {1}", match.Value, match.Index); } } // The example displays the following output: // 'quity' found at position 1 // 'quip' found at position 14 // 'quaint' found at position 21
出处:http://qixuejia.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2011-01-12 C#获取本地计算机名,IP,MAC地址,硬盘ID