一、纯文本搜索示例
using System.Text.RegularExpressions;
namespace chapter9
{
class Program
{
static void Main(string[] args)
{
const string myText = @"No pains, no gains";
const string pattern = "ns";
// MatchCollection 为Match对象的集合。Matchs为Regex类的静态方法,返回Match对象
MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
foreach (Match nextMatch in myMatches)
Console.WriteLine(nextMatch.Index); // 输出相应对面的下标
Console.ReadLine();
}
}
}
二、 正则表达式
正则表达式语言由两种基本字符类型组成:原义(正常)文本字符和元字符。元字符使正则表达式具有处理能力。所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式。元字符*用来匹配0个或多个的前一字符;而元字符.用来匹配一个任意的一个字符(除换行符外)。
\b 表示字符边界 , \B 非字符边界, \d表示数值边界, \D表示非数值边界,
const string pattern = @"\ba"; //以a开头的模式
const string pattern = @"a\b"; //以a结尾的的模式, @确保‘\’不被C#编译器解释为转义序列
const string pattern = @"\ba\S*ion\b"; //表示查找以啊开头,ion结尾的单位, \S表示任何非空白字符, * 限定符,表示前面的字 // 符可以重复任意次, \S*表示任意个非空白字符 (\s表示空白字符)
tips:
1、*(任意次),+(>=1次),?(0-1次),表示字符重复的次数,两两包含关系;
2、^, $ 输入文本的开头和结尾、
3、. 除了换行符以外的所有单个字符
4. 可以吧替换字符放在[]中,如ma[n|p], 表示请求匹配man/map的字符 , [a-z],[0-9], [0-9]+,表示请求匹配的字母、数字范围
示例:现在匹配字符位置索引, 匹配字符, 较长字符串(可以根据需要精准定位,便于快速查找)
namespace chapter9
{
class Program
{
static void Main(string[] args)
{
const string myText = @"No pains, no gains";
const string pattern = "ns";
MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
WriteMatches(myText, myMatches);
Console.ReadLine();
}
// 确定在较长的字符串中有多少字符可以显示,而无须超出输入文本的开头或者结尾
static void WriteMatches(string text, MatchCollection matches)
{
Console.WriteLine("Original text:\n\n {0} \n", text);
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},\t String:{1}, \t {2}", index, result, text.Substring(index - charsBefore, charsToDisplay));
}
}
}
}
static void Main(string[] args)
{
// 提取电话号码
const string myText = @" ashdklfhkjashdkfj13808298023aksdhfkasdhfkh18628065289asdf1816128928912398123asdf12313";
const string pattern = @"([1-9])([0-9]{10})"; // 等价于 @"([1-9])(\d{10})"
MatchCollection myMatch = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase);
foreach (Match nextMath in myMatch)
{
Console.WriteLine(nextMath.ToString());
}
Console.ReadLine();
//提取单词
const string myText = @"application";
const string pattern = @"\ba\S*n\b"; // const string pattern = @"[a-z]+";提取小写字母
MatchCollection myMatch = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase);
foreach (Match nextMath in myMatch)
{
Console.WriteLine(nextMath.ToString());
}
Console.ReadLine();*/
}
示例二:
http:\\www.wrox.com:4355
表达式:\b(\S+) :\\ ([^:]+) (?::(\S+))? \b // 匹配、组合、捕获