-1
我想找出多少次()模式出现在一个字符串;这里是代码:奇怪的错误,而使用Regex.Matches
int pMatches = (Regex.Matches("(", newDrug).Count + Regex.Matches(")", newDrug).Count)/2;
我收到以下错误:
Quantifier {x,y} following nothing
为什么?提前致谢。
我想找出多少次()模式出现在一个字符串;这里是代码:奇怪的错误,而使用Regex.Matches
int pMatches = (Regex.Matches("(", newDrug).Count + Regex.Matches(")", newDrug).Count)/2;
我收到以下错误:
Quantifier {x,y} following nothing
为什么?提前致谢。
你有两个问题。
首先是你有相反的顺序Regex.Matches()
的参数。这会导致输入字符串被视为模式,并将"("
视为与其匹配的文本。 (似乎您的输入可以与{
字符,这使得它无效的正则表达式,因此错误消息开始。)
Regex.Matches(newDrug, "(");
第二是(
是一个特殊的正则表达式字符和需要转义:
Regex.Matches(newDrug, Regex.Escape("("))
非常感谢。 – bob 2014-10-01 21:14:09
请提供** entire **错误消息。这只是一小部分。 – omni 2014-10-01 21:11:08