6

I have written a c program that retrieves arguments from the command line under Windows. One of the arguments is a regular expression. So I need to retrieve special characters such as "( , .", etc., but cmd.exe treats "(" as a special character.

How could I input these special character?

thanks.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
Jichao
  • 40,341
  • 47
  • 125
  • 198

2 Answers2

10

You can put the arguments in quotes:

myprogram.exe "(this is some text, with special characters.)"

Though I wouldn't assume that parentheses cause problems unless you are using blocks for conditional statements or loops in a batch file. The usual array of characters that are treated specially by the shell and need quoting or escaping are:

& | > < ^

If you use those in your regular expression, then you need quotes, or escape those characters:

myprogram "(.*)|[a-f]+"
myprogram (.*)^|[a-f]+

(^ is the escape character which causes the following character to be not interpreted by the shell but instead used literally)

Joey
  • 344,408
  • 85
  • 689
  • 683
10

You can generally prefix any character with ^ to turn off its special nature. For example:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Pax>echo No ^<redirection^> here and can also do ^
More? multi-line, ^(parentheses^) and ^^ itself
No <redirection> here and can also do multi-line, (parentheses) and ^ itself

C:\Documents and Settings\Pax>

That's a caret followed by an ENTER after the word do.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Note that you don't need to escape parentheses unless you are using this in an conditional or loop block such as `if foo==bar ( echo ^(foo^) )` – Joey Nov 16 '09 at 11:41
  • @joey - is it still possible to escape it? Not escaping that might lead to problems, if someone unmindfully does a copy/paste... – Archangel1C Feb 10 '20 at 13:17