开发者

C# Regex - How to ignore escape sequences with variables

Say I have this code:

foreach (string filepath in someList)
{
    someBool = Regex.IsMatch(someString, filepath);
}

Where someBool, someList, and someString are just a random boolean, list, and string, respectively (This is a simple example of what I'm trying to do). Filepath is a filepath, with a bunch of backslashes (i.e. C:\\somefolder\somefile). The problem is by running this code, I get an ArgumentException error, with an "unrecognized escape sequen开发者_开发百科ce" problem for things like "D:\\H..." I tried using

someBool = Regex.IsMatch(someString, @filepath);

and I am still seeing the error. Is there something else I'm forgetting?


Have you tried using Regex.Escape

Regex.IsMatch(someString, Regex.Escape(filepath));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜