0

I have difficulties understanding how to specify special characters ^ and " as parameters in windows command prompt. I am using gnuwin32's diff in Win 7 . It has a -I option that accepts a regular expression from the command line. I want to invoke diff in a batch file specifying a regular expression. However, my regular expression is a complex one with ^ " and white-space special characters within it:

^[ \t]*#[ \t]*include[ \t]+"c:\\program files

I'm not able to understand Windows's rules for specifying special characters in the command line in this case which has special rules regarding ^ and ". How am i going to specify the above regular expression in the command line?

I guess it should look like this:

diff -I "^[ \t]*#[ \t]*include[ \t]+"c:\\program files" file1 file2

But the " in the middle mess up the whole thing. How should I deal with the ^ and "?

JavaMan
  • 4,954
  • 4
  • 41
  • 69
  • `echo ^^[ \t]*#[ \t]*include[ \t]+"c:\\program files` would match your string, or try passing it as is but in quotes. – Alex K. Apr 14 '16 at 13:17
  • no, I need to give diff.exe the above string as argument i.e.in the command line: diff -I ^[ ..."... file1 file2. This will fail if I just type it in the command prompt as it contains ^ " and white-space which requires special sequences that I am not able to understand how to specify in windows (the rules are all too complex and inconsistent for me to understand) – JavaMan Apr 14 '16 at 13:23

1 Answers1

0

After some research and testing, this should be the regex required:

diff -I "^[[:space:]]*#[[:space:]]*include[[:space:]]\+\"c:\\\\program file" file1 file2

For one thing, diff is using grep style regex which doesn't support the Perl style \t sequence. We need to use \" to specify a literal ".

Also, do not use windows batch file to test the parameter passed to a program as I did.

In other words, never think this batch file diff.bat with a few echos can help you test which command line arguments to use:

echo %1
echo %2
echo %3
echo %4

Write and compile a program for this and have it dump the command line arguments.

JavaMan
  • 4,954
  • 4
  • 41
  • 69
  • Note that since it is the application that parses the command line into tokens (or not, as the case may be) there is no guarantee that application A will produce the same tokens as application B. In this case, gnuwin32 probably uses the Microsoft C runtime parser (although this was probably not the best choice!) so writing your own C program is the appropriate option. In other cases, you may need to use guesswork, or consult the source code or vendor documentation. – Harry Johnston Apr 15 '16 at 00:37
  • Oh, one further complication: the caret is cmd.exe's quote character, but like all special characters it only applies outside of double-quote marks. So the caret in the string you've shown is taken literally because it is between the first two double-quote marks. If you had to put a caret after the second double-quote mark, you'd need to double it up. – Harry Johnston Apr 15 '16 at 21:48