[英]Match with reges excluding string tags
我正在嘗試編寫代碼以獲取列表中的匹配項,但沒有匹配項標記,直到現在我已經在用C#編寫的WP7應用程序中構建了以下代碼
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;
}
那么如何在匹配提取后不使用replace函數的情況下不使用string_start,string_end(匹配標簽)返回匹配?
使用環顧四周。
String.Format("(?<={0}).*?(?={1})",string_start,string_end);
盡管您也可以使用groups.ie,但在正則表達式(.*?)
中將捕獲組1中的內容。然后不需要環顧四周。
MatchTags(content,start,end).Cast<Match>()
.Select(x=>x.Groups[1].Value);
當我得到下一個代碼的結果時,它起作用:
string my_string_no_tags = matches[number].Groups[1].Value;
考慮以下代碼...
MatchCollection matches = Regex.Matches(content, string.Format("(?<={0}).*?(?={1})", string_start, string_end), RegexOptions.IgnoreCase);
return matches;
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.