Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Regex.Matches时出现奇怪的错误_C#_Regex - Fatal编程技术网

C# 使用Regex.Matches时出现奇怪的错误

C# 使用Regex.Matches时出现奇怪的错误,c#,regex,C#,Regex,我试图找出()模式在字符串中出现的次数;代码如下: int pMatches = (Regex.Matches("(", newDrug).Count + Regex.Matches(")", newDrug).Count)/2; 我得到以下错误: Quantifier {x,y} following nothing 为什么??提前谢谢。您有两个问题 第一个是,你有相反顺序的参数。这将导致输入字符串被视为模式,”(“被视为与之匹配的文本。) 第二个是(是一个特殊的正则表达式字符,需要转义:

我试图找出()模式在字符串中出现的次数;代码如下:

int pMatches = (Regex.Matches("(", newDrug).Count + Regex.Matches(")", newDrug).Count)/2;
我得到以下错误:

Quantifier {x,y} following nothing
为什么??提前谢谢。

您有两个问题

第一个是,你有相反顺序的参数。这将导致输入字符串被视为模式,
”(“
被视为与之匹配的文本。)

第二个是
是一个特殊的正则表达式字符,需要转义:

Regex.Matches(newDrug, Regex.Escape("("))
请提供整个错误消息。这只是一小部分。
Regex.Matches(newDrug, Regex.Escape("("))