简体   繁体   English

为什么此正则表达式不正确匹配?

[英]Why does this Regex not match properly?

I basically have this: 我基本上有这个:

Regex rx = new Regex(@"\$(?:(\$)|(\{(?<ex>.*?)\}))");
string s = "${P#(n*8+1)!=0$$P}${P#(n*8+1)!=0$N/A$[n*8+1]}";

Match m = rx.Match(s, 0);

The first match is "${P#(n*8+1)!=0$N/A$[n*8+1]}" when it should be "${P#(n*8+1)!=0$$P}" . 当它应为"${P#(n*8+1)!=0$$P}" "${P#(n*8+1)!=0$N/A$[n*8+1]}"时,第一个匹配为"${P#(n*8+1)!=0$N/A$[n*8+1]}" "${P#(n*8+1)!=0$$P}" If I put an extra space before the first '$' , it works fine. 如果我在第一个'$'之前放置多余的空格,则可以正常工作。

You are swapping the arguments. 您正在交换参数。 Regex.IsMatch signature is: Regex.IsMatch签名为:

public static bool IsMatch(string input, string pattern)

EDIT: the following code prints True twice for me. 编辑:以下代码为我两次打印True

var p = @"\$(?:(\$)|(\{(?<ex>.*?)\}))";
var regex = new Regex(p);
Console.WriteLine(regex.IsMatch(" ${foo}"));
Console.WriteLine(regex.IsMatch("${foo}"));

EDIT2: deleted the previous edit, the match works for me. EDIT2:删除了之前的修改,该匹配对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM