where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') ))
");
用string.format格式化正则的时候报这个错误,请求解决
“System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生
其他信息: 索引(从零开始)必须大于或等于零,且小于参数列表的大小。
8 个解决方案
#1
特别对于花括号,format是有特殊要求的,花括号里的视为格式化的参数,你这个正则里一堆花括号,肯定出错,记得转义,把所有非格式化参数的花括号等一些特殊字符,用 反斜杠转义 \{ \}
#2
string fa = string.Format("{0}",@"
where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') ))
");
where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') ))
");
#3
{ =》 {{
} =》 }}
} =》 }}
#4
对的,是这样
#5
patten = string.Format(@"[a-z]{0}{{1,2}}", "a");
结果是
[a-z]a{1,2}
结果是
[a-z]a{1,2}
#6
注意转义 楼主加强 基础知识学习啊
#7
string fa = string.Format(@"
where (((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)')) OR
(REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[[3|4|5|8]][[0-9]]\d{{8}}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}$)')) OR
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)') ))
");
where (((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)')) OR
(REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[[3|4|5|8]][[0-9]]\d{{8}}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}$)')) OR
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)') ))
");
#8
原句如此的话,没必要string.Format的,直接:
string fa = @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
也可自己拼接:
string fa = string.Format("abc", abc) + @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
string fa = @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
也可自己拼接:
string fa = string.Format("abc", abc) + @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
#1
特别对于花括号,format是有特殊要求的,花括号里的视为格式化的参数,你这个正则里一堆花括号,肯定出错,记得转义,把所有非格式化参数的花括号等一些特殊字符,用 反斜杠转义 \{ \}
#2
string fa = string.Format("{0}",@"
where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') ))
");
where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') ))
");
#3
{ =》 {{
} =》 }}
} =》 }}
#4
对的,是这样
#5
patten = string.Format(@"[a-z]{0}{{1,2}}", "a");
结果是
[a-z]a{1,2}
结果是
[a-z]a{1,2}
#6
注意转义 楼主加强 基础知识学习啊
#7
string fa = string.Format(@"
where (((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)')) OR
(REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[[3|4|5|8]][[0-9]]\d{{8}}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}$)')) OR
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)') ))
");
where (((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)')) OR
(REGEXP_LIKE(T.HOMEPHONE, '(^0\d{{2,3}}-\d{{7,8}}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[[3|4|5|8]][[0-9]]\d{{8}}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}$)')) OR
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{{2,3}}-\d{{7,8}}-\d{{1,4}}$)') ))
");
#8
原句如此的话,没必要string.Format的,直接:
string fa = @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
也可自己拼接:
string fa = string.Format("abc", abc) + @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
string fa = @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";
也可自己拼接:
string fa = string.Format("abc", abc) + @" where ((REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') OR
REGEXP_LIKE(T.HOMEPHONE, '(^0\d{2,3}-\d{7,8}$)') ) or
(REGEXP_LIKE(T.MOBILE, '(^1[3|4|5|8][0-9]\d{8}$)') ) or
(REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}$)') OR
REGEXP_LIKE(T.OFFICE_TEL, '(^0\d{2,3}-\d{7,8}-\d{1,4}$)') )";