Link to home
Start Free TrialLog in
Avatar of kasperEH
kasperEHFlag for Denmark

asked on

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");
                }
                else
                {
                    this.CopyToSharepoint(currentDir.FullName,fi.Name, fi.LastWriteTime);
                }

Can anybody tell me why? (and yes, some of the files start with ~$)
ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kasperEH

ASKER

Thanks. I should have found that mistake myself :-)