Why does findstr not handle case properly (in some circumstances)?

后端 未结 4 1436
名媛妹妹
名媛妹妹 2020-11-29 07:08

While writing some recent scripts in cmd.exe, I had a need to use findstr with regular expressions - customer required standard cmd.exe commands (no GnuWin32 no

4条回答
  •  执笔经年
    2020-11-29 07:34

    This appears to be caused by the use of ranges within regular expression searches.

    It doesn't occur for the first character in the range. It doesn't occur at all for non-ranges.

    > echo a | findstr /r "[A-C]"
    > echo b | findstr /r "[A-C]"
        b
    > echo c | findstr /r "[A-C]"
        c
    > echo d | findstr /r "[A-C]"
    > echo b | findstr /r "[B-C]"
    > echo c | findstr /r "[B-C]"
        c
    
    > echo a | findstr /r "[ABC]"
    > echo b | findstr /r "[ABC]"
    > echo c | findstr /r "[ABC]"
    > echo d | findstr /r "[ABC]"
    > echo b | findstr /r "[BC]"
    > echo c | findstr /r "[BC]"
    
    > echo A | findstr /r "[A-C]"
        A
    > echo B | findstr /r "[A-C]"
        B
    > echo C | findstr /r "[A-C]"
        C
    > echo D | findstr /r "[A-C]"
    

    According to the SS64 CMD FINDSTR page (which, in a stunning display of circularity, references this question), the range [A-Z]:

    ... includes the complete English alphabet, both upper and lower case (except for "a"), as well as non-English alpha characters with diacriticals.

    To get around the problem in my environment, I simply used specific regular expressions (such as [ABCD] rather than [A-D]). A more sensible approach for those that are allowed would be to download CygWin or GnuWin32 and use grep from one of those packages.

提交回复
热议问题