我正在使用(。)匹配所有字符,但它也返回太多匹配。我如何製作它只有1場比賽?正則表達式返回所有字符
private MatchCollection RegexMatchingV2(string data, string regex)
{
MatchCollection col = null;
try
{
col = Regex.Matches(data, regex, RegexOptions.IgnoreCase);
}
catch (Exception ex)
{
Response.Write("RegexMatching ERROR:" + ex.Message);
}
return col;
}
protected void Page_Load(object sender, EventArgs e)
{
MatchCollection col= RegexMatchingV2("return all of this data in 1 match", "(.)");
Response.Write(col.Count);//Too much matches
}
你的目標是什麼?不加區別地匹配整個字符串是毫無意義的操作。 – Douglas
只是懶得寫很多條件語句 –
什麼條件語句?如何在「MatchCollection」中將整個字符串作爲單個「匹配」獲取幫助? – Douglas