日期:2014-05-17 浏览次数:21005 次
string str = System.IO.File.ReadAllText("hello [index]student, welcome to [index]regex world!");
Regex regx = new Regex("index");
Match mat = regx.Match(str);
while(mat.Success)
{
MessageBox.Show(mat.Index.ToString());//位置
mat = regx.Match(str, mat.Index+mat.Length);
}
string str = "hello [index]student, welcome to [index]regex world!";
var result = Regex.Matches(str, @"(?i)(?<=\[index\])[^\[\]]+").OfType<Match>().Select(a => a.Value).ToList();
/*
[0] "student, welcome to " string
[1] "regex world!" string
*/