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));
精彩评论