【问题标题】:How to input special character in cmd?如何在cmd中输入特殊字符?
【发布时间】:2009-11-16 11:17:07
【问题描述】:

我编写了一个 c 程序,可以在 Windows 下从命令行检索参数。其中一个参数是正则表达式。所以我需要检索特殊字符如“(,.”等,但是cmd.exe把“(”当成特殊字符。

如何输入这些特殊字符?

谢谢。

【问题讨论】:

    标签: shell cmd


    【解决方案1】:

    您通常可以在任何字符前加上^ 以关闭其特殊性质。例如:

    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>
    

    这是一个插入符号,在单词 do 后面跟着一个 ENTER

    【讨论】:

    • 请注意,您不需要转义括号,除非您在条件或循环块中使用它,例如if foo==bar ( echo ^(foo^) )
    • @joey - 还有可能逃脱吗?如果有人不小心复制/粘贴,不逃避可能会导致问题......
    【解决方案2】:

    您可以将参数放在引号中:

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

    虽然我不认为括号会导致问题除非您在批处理文件中使用块用于条件语句或循环。通常由 shell 特殊处理并需要引用或转义的字符数组是:

    & | > < ^
    

    如果你在你的正则表达式中使用这些,那么你需要引号,或者转义这些字符:

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

    ^转义字符,它导致后面的字符不被 shell 解释,而是按字面意思使用)

    【讨论】:

    • 我认为^ 也需要转义。您应该将其添加到您的列表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2021-10-31
    相关资源
    最近更新 更多