简体   繁体   中英

Pass \ in command line arguments of C++

I am working on a C++ program where one of the command line arguments needs to be a passed a regex. For example: abc.exe --option ab\\[0\\]

When I access the option value from inside the program, it becomes ab\\\\[0\\\\] which becomes a different regex. Inside the program when I try to replace \\\\[ with \\[ using boost::replace_all , the result is [ which also is not the intended output for me.

So, any suggestions on how to pass and retain \\[ this while passing it through command line arguments

You can quote the parameter:

abc.exe --option "ab\[0\]"

Or use the shell escape sequence:

abc.exe --option ab\\[0\\]

Did you try these?

It was a problem with how visual studio displays the symbol. When I looked on the ASCII code of the symbol, it was alright. Thanks @ScottK for helping me to debug this through your comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM