3

I compiled the code:

namespace TestRegExp
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Regex.IsMatch(args[1], args[0]))
                Console.WriteLine("Input matches regular expression.");
            else
                Console.WriteLine("Input DOES NOT match regular expression.");
        }
    }
}

When I run:

  • TestRegExp.exe ^a\d{5}$ a12345 shows Input matches regular expression.
  • TestRegExp.exe ^a\d{5}$ aa12345 shows Input matches regular expression.
  • TestRegExp.exe ^^a\d{5}$ a12345 shows Input matches regular expression.
  • TestRegExp.exe ^^a\d{5}$ aa12345 shows Input DOES NOT match regular expression.

Why the second option shows Input matches regular expression.?

The '^' symbol represents the string init... alright? and why do I have to repeat this?

1
  • What CLI did you use? It sounds like command line interpretation problem. Commented Jun 9, 2013 at 1:27

2 Answers 2

8

The ^ is used as an escape character in the Windows commandline environment. It tells the command interpreter to treat the next as a literal character (since some characters like <, > and | have special meanings otherwise).

^a evaluates to a when parsed.

^^ evaluates to ^ when parsed.

Sign up to request clarification or add additional context in comments.

Comments

7

This has nothing to do with the regular expression itself.

If you print args[0] to the console, you'll see that it doesn't contain the ^. This is because Windows parses it as an escape character if the expression is not quoted.

If you call it like this:

TestRegExp.exe "^a\d{5}$" aa12345

You'll get the expected result.

1 Comment

Can you clarify why the ^ is not included?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.