Match m = null; Regex r = new Regex("bc(.*?)45", RegexOptions.IgnoreCase | RegexOptions.Compiled); List<string> tmpList = new List<string> for (m = r.Match(content); m.Success; m = m.NextMatch()) { tmpList.Add(m.Groups[0].Value); }
string aa="abcdefghijklmn0123456789";for (m = r.Match(aa); m.Success; m = m.NextMatch())
则多个都需要返回bcdefghij45 和 bcdefij45
第二种方法,求字符串字串的方法。这个方法简单,但是有点繁琐。你可以这样做
思路:
int start = aa.IndexOf("bc");
int end=aa.IndexOf("45");
string newstring=aa.Substring(start+2,end-start-2);start=aa.IndexOf("bc",end);//寻找第二个
end=aa.IndexOf("45",end);//寻找第二个
...
这里你可以写个while循环找出所有的如
IList<string> list=new List<string>();
while(end!=-1)
{
start=aa.IndexOf("bc",end);//寻找第二个
end=aa.IndexOf("45",end);//寻找第二个
list.Add(aa.Substring(start+2,end-start-2));
}
大体就是这样了。
string str = "abcdefghij456 bcdefij456";
foreach (Match reg in Regex.Matches(str, @"bc\w+?45"))
{
Console.WriteLine(reg.Value);
}
Regex r = new Regex("bc(.*?)45", RegexOptions.IgnoreCase | RegexOptions.Compiled);
List<string> tmpList = new List<string>
for (m = r.Match(content); m.Success; m = m.NextMatch())
{
tmpList.Add(m.Groups[0].Value);
}