开发者

C# Regex - How to ignore escape sequences with variables

开发者 https://www.devze.com 2023-03-13 15:17 出处:网络
Say I have this code: foreach (string filepath in someList) { someBool = Regex.IsMatch(someString, filepath);

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

精彩评论

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