Wherever you got that code, you didn't think before you modified it.
1) it doesn't try to check the file content: this line:
var file = filepath;
does not read anything from the file.
You could try:
var file = File.ReadAllText(filepath);
2) Your regex matches everything. '.' in a Regex means "any character at all" so having a dot in your regex means it will always match every character:
Is functionally identical to
This needs to be escaped with '\.'
3) Even if you fix that, the regex contains "special characters" like '^' - "start of line or string", and '$' - "end of line or string". Special characters need to be escaped in the same way that '.' needed to be.
4) Inside your loop: what do you think that does? your regex matches individual characters, and you then assume that they are automatically valid regexes?
This is the code that generates the error message: as soon as you regex matches the first '\' - which it will, very quickly given that you existing code parses the file path rather than the file content - the regex processor fails because that's an escape character and it needs to be followed by the character you are escaping.
Even if I believe that you aren't the person who posted this question twice already - which would be a HUGE
leap of faith - then you need to learn that you can't just clip bits of code out, slam them together and get what you want: you need to think about what the code you are given does, and how that might form a basis for you requirements. You will almost never find code that you can just copy, paste, and submit as your homework and expect it to work as is.