How to write a search pattern to include a space in findstr?

后端 未结 4 1483
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 20:02

I want to search all files in a certain directory for occurrences of statements such as

  Load frmXYZ

I am on Windows 7, using the f

4条回答
  •  再見小時候
    2020-12-29 20:39

    Use word delimiter regex

    I used the the special \< "beginning of word" regex symbol.

    I tried this on the Win10 version of findstr. But according to Microsoft this special \< symbol has been in findstr.exe ever since WinXP.

    Full (and painful) breakdown of many options that do NOT work below.

    At the very bottom: what actually worked.

    The sample file itself

    C:\>type lines.txt
    Load frmXYZ                         // This line should match.
    If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
    pears Load frm grapes pineapples    // This line should match.
                                        // This blank line should NOT match.
    LOAD FRMXYZ                         // This line should match.
    IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
    PEARS LOAD FRM GRAPES PINEAPPLES    // This line should match.
                                        // This blank line should NOT match.
    load frmxyz                         // This line should match.
    if abcformloaded then unload frmpqr // This line should NOT match.
    pears load frm grapes pineapples    // This line should match.
    

    Wrong. With regular execution space is treated as delimiter.

    C:\>type lines.txt | findstr /N "Load frm"
    1:Load frmXYZ                         // This line should match.
    2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
    3:pears Load frm grapes pineapples    // This line should match.
    9:load frmxyz                         // This line should match.
    10:if abcformloaded then unload frmpqr // This line should NOT match.
    11:pears load frm grapes pineapples    // This line should match.
    

    Wrong: With Regex option space is STILL treated as delimiter.

    C:\>type lines.txt | findstr /N /R "Load frm"
    1:Load frmXYZ                         // This line should match.
    2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
    3:pears Load frm grapes pineapples    // This line should match.
    9:load frmxyz                         // This line should match.
    10:if abcformloaded then unload frmpqr // This line should NOT match.
    11:pears load frm grapes pineapples    // This line should match.    
    

    More right but still wrong. With /C option we now get preserved spaces but don't find other character cases.

    C:\>type lines.txt | findstr /N /R /C:"Load frm"
    1:Load frmXYZ                         // This line should match.
    3:pears Load frm grapes pineapples    // This line should match.
    

    Wrong. /I for "Ignore Case" does not help. We get matches from within words we did not want.

    C:\>type lines.txt | findstr /N /R /I /C:"Load frm"
    1:Load frmXYZ                         // This line should match.
    2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
    3:pears Load frm grapes pineapples    // This line should match.
    5:LOAD FRMXYZ                         // This line should match.
    6:IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
    7:PEARS LOAD FRM GRAPES PINEAPPLES    // This line should match.
    9:load frmxyz                         // This line should match.
    10:if abcformloaded then unload frmpqr // This line should NOT match.
    11:pears load frm grapes pineapples    // This line should match.
    

    Right. Use special "Beginning of word" regex symbol. Matches beginning-of-line or space.

    Either case sensitive:

    C:\>type lines.txt | findstr /N /R /C:"\

    or ignoring case

    C:\>type lines.txt | findstr /N /R /I /C:"\

提交回复
热议问题