日期:2014-05-17  浏览次数:20964 次

请教一个正则表达式查找替换文本的问题!
本帖最后由 zhouxicai 于 2013-07-03 14:22:07 编辑
如果用正则表达式找到了标记文本,我想读取标记文本之后的文本该如何操作?

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


                 */