Regular expression only working in command prompt, not as variable

I want to check if a file name contains ~$ (tilde and dollar) as the first two characters, because then it's an Office backup file. That should be the following regular expression:  ^\x7E\x24

When I create a c# program where I have to type in the regular expression in the command prompt like this it works fine:
Enter regular expression: ^\x7E\x24
Enter input for comparison: ~$rbar i Nordic.doc

However, when I use the same regular expression as a string variable in a c# program where fi is FileInfo, it doesn't work:
string reTildeDollar = @"^\x7E\x24";
if (Regex.IsMatch(reTildeDollar, fi.Name))
                    Console.WriteLine("Office backup file - will not be copied");
                    this.CopyToSharepoint(currentDir.FullName,fi.Name, fi.LastWriteTime);

Can anybody tell me why? (and yes, some of the files start with ~$)
Who is Participating?
saraganiConnect With a Mentor Commented:
Hi, it should be:
 if (Regex.IsMatch(fi.Name, reTildeDollar))

You have switched the order of the regex match parameters.
The first parameter should be the input string and the second one is the regex expression, not the oppssite.
Fernando SotoConnect With a Mentor RetiredCommented:
Hi kasperEH;

It is because you have the parameters to the method backwards, it should be this.

Regex.IsMatch(fi.Name, reTildeDollar)

kasperEHAuthor Commented:
Thanks. I should have found that mistake myself :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.