Regular expression only working in command prompt, not as variable

Posted on 2011-05-13
Last Modified: 2012-05-11
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 ~$)
Question by:kasperEH
    LVL 11

    Accepted Solution

    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.
    LVL 62

    Assisted Solution

    by:Fernando Soto
    Hi kasperEH;

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

    Regex.IsMatch(fi.Name, reTildeDollar)


    Author Closing Comment

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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now