2013-11-22 24 views
1

我想写以获取列表,但没有匹配标签相匹配的代码,到现在为止我已经建立了WP7应用程序下面的代码写在C#如同reges排除字符串变量

public static MatchCollection MatchTags(string content, string string_start, string string_end) 
{ 
    MatchCollection matches = Regex.Matches(content, string_start + "(.*?)" + string_end, RegexOptions.IgnoreCase); 
    return matches; 
} 

那么如何在匹配提取后不使用替换函数的情况下返回没有使用string_start,string_end(match tags)的匹配项呢?

回答

1

它的工作原理当我得到下一个代码的结果:

string my_string_no_tags = matches[number].Groups[1].Value; 
+0

这是整个时间的答案,但有些人希望探索lookaround断言作为一种学习体验。但是断言可能会带来更多的时间上的开销。就这样你知道。 – sln

+0

就是这样!感谢您的支持者! x.Groups [1]。价值; –

1

使用lookarounds ..

String.Format("(?<={0}).*?(?={1})",string_start,string_end); 

虽然你也可以在你的正则表达式使用groups.ie (.*?)将捕获1组无需lookarounds那么内的内容..

MatchTags(content,start,end).Cast<Match>() 
          .Select(x=>x.Groups[1].Value); 
+0

我看到我无法在wp7上使用MatchTags,也感谢您的评论! –

0

考虑下面的代码...

MatchCollection matches = Regex.Matches(content, string.Format("(?<={0}).*?(?={1})", string_start, string_end), RegexOptions.IgnoreCase); 
return matches; 

祝您好运!