读书人

请问一个正则表达式查找替换文本的有关

发布时间: 2013-07-08 14:13:00 作者: rapoo

请教一个正则表达式查找替换文本的问题!
本帖最后由 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


*/

读书人网 >C#

热点推荐