Reputation: 11
I'm working on a virus / ransomware early warning system for our facility that compares filenames and paths collected from a FileSystemMonitor against a list of regular expressions from an SQL database.
The issue I am having is with C# escaping my regex expressions as they are read from the SQL server.
For example:
SqlCommand command = new SqlCommand("SELECT * FROM Filters ORDER BY Sequence ASC", sqlServer);
SqlDataReader reader = null;
reader = command.ExecuteReader();
filter temp;
while (reader.Read())
{
temp.dir = @reader["Folder"].ToString().Trim();
temp.file = @reader["Filter"].ToString().Trim();
if (!temp.file.Equals(""))
{
filterList.Add(temp);
}
}
If the SQL field Filter
contains the valid regex .*\.scr$
, temp.filter
ends up with the contents .*\\.scr$
which causes the regex match to be missed.
How can I maintain the verbatim state of my regex when read from SQL?
Thanks in advance for any assistance!
Upvotes: 0
Views: 635
Reputation: 11
Just wanted to verify that this question has been resolved. Including @
in front of the reader commands is having the desired effect, it's just that I was viewing the strings via the debug environment which was confusing me to think otherwise.
Thanks for everyone's input! Was going to upvote the helpful comments, but it looks like I don't have enough rep yet for that.
Upvotes: 1