简体   繁体   中英

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}" . If I put an extra space before the first '$' , it works fine.

You are swapping the arguments. Regex.IsMatch signature is:

public static bool IsMatch(string input, string pattern)

EDIT: the following code prints True twice for me.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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