[英]Regex Nested Quantifier exception while matching regular expression.
int characterLimit = 5;
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}+$");
if(!string.IsNullOrEmpty(e.NewTextValue))
if (!Regex.IsMatch( e.NewTextValue, regxForAlpha)){
}
else
{
}
此代碼引發NestedQuantifier異常。 誰能知道為什么?
這是固定代碼:
string NewTextValue = "str";
int characterLimit = 5;
string regxForAlpha = "^[a-zA-Z \n]{0,"+characterLimit.ToString()+"}$";
if(!string.IsNullOrEmpty(NewTextValue))
if (!Regex.IsMatch( NewTextValue, regxForAlpha)){
Console.WriteLine("No match");
}
else
{
Console.WriteLine("Match");
}
見IDEONE演示 (改變e.NewTextValue
到NewTextValue
用於演示目的)。
有幾個興趣點:
Regex.IsMatch
接受字符串 (不是Regex對象)作為其第二個參數 {0,5}+
-且+
導致嵌套量詞問題)。 [a-zA-Z \\n] {0,5}
, {0,5}
將應用於左側旁邊的空格 ,正則表達式的含義是有點扭曲。 請更換
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}+$");
至
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}$");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.