37

特定のディレクトリ内のすべてのファイルで、次のようなステートメントの出現を検索したい

  Load frmXYZ

私はWindows7を使用しており、findstrコマンドを使用しています。私は試した:

  findstr /n Load.*frm *.*

しかし、これは私に次のような望ましくない結果をもたらします:

 If ABCFormLoaded Then Unload frmPQR

Loadそこで、との間に空白を入れて、次のfrmようなコマンドを実行しました。

 findstr /n Load frm *.*

loadしかし、これは単に単語のすべての出現または単語のすべての出現を検索しましたfrm。この問題を回避するにはどうすればよいですか?

4

4 に答える 4

40

スペースを使用する場合/C:は、リテラル文字列を正規表現/Rオプションに渡すオプションが必要です。
正規表現に到達すると、正規表現として扱われます。

とはいえ、これは典型的なMSのゴミです。

2つの正規表現検索文字列を使用する


Load frm肝心なのは、次のように最初にある場合を処理するには、2つの文字列を使用する必要があるということです。

  • Load frm apples bananas carrots

またはそのように真ん中に:

  • some other text Load frm and more

文字クラスのないバージョン

以下はXPsp3を使用しています、Windows 7は異なる場合があり、どちらもゴミです!

findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt

7:Load frm is ok    
8:    Load     frm is ok  

コロンに注意

注:のコロン/C:は、これが機能するために必須です。

コロンを省略した場合findstrのエラー処理は、無効なオプションとして扱うだけ/Cです。その無効なオプションを無視して、とにかく先に進んでください。予期しない不要な出力につながります。

文字クラスを使用した同等のバージョン

findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt

キャラクタークラスの内訳

// The first regex search string breaks down like this:
[ ]   // require 1 space
[ ]*  // optional many spaces
Load  // literal 'Load'
[ ]   // require 1 space
[ ]*  // optional many spaces
frm   // literal 'frm'

// The second regex search string breaks down like this:
^     // beginning of line
Load  // literal 'Load'
[ ]   // require 1 space
[ ]*  // optional many spaces
frm   // literal 'frm'

本当の正規表現は\bLoad\s+frm

于 2012-03-20T17:01:16.510 に答える
31

/c次のオプションを使用します。

findstr /n /c:"Load frm" *.*

ヘルプから(findstr /?):

/C:string  Uses specified string as a literal search string.
于 2012-03-20T15:19:21.147 に答える
8

単語区切り文字の正規表現を使用する

特別な\<「単語の始まり」の正規表現記号を使用しました。

私はfindstrのWin10バージョンでこれを試しました。しかし、Microsoftによると、この特別な\<シンボルはfindstr.exeWinXP以来ずっと存在しています。

以下では機能しない多くのオプションの完全な(そして苦痛な)内訳。

一番下:実際に機能したもの。

サンプルファイル自体

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.

間違い。通常の実行スペースでは、区切り文字として扱われます。

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.

間違った例:正規表現オプションを使用すると、スペースは引き続き区切り文字として扱われます。

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.    

もっと正しいが、それでも間違っている。/ Cオプションを使用すると、スペースが保持されるようになりましたが、他の文字の大文字小文字は見つかりません。

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.

間違い。「ケースを無視」の/Iは役に立ちません。望まない単語の中から一致を取得します。

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.

右。特別な「単語の始まり」正規表現記号を使用します。行頭またはスペースに一致します。

大文字と小文字の区別:

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.

または大文字と小文字を区別しない

C:\>type lines.txt | findstr /N /R /I /C:"\<Load frm"
1:Load frmXYZ                         // This line should match.
3:pears Load frm grapes pineapples    // This line should match.
5:LOAD FRMXYZ                         // This line should match.
7:PEARS LOAD FRM GRAPES PINEAPPLES    // This line should match.
9:load frmxyz                         // This line should match.
11:pears load frm grapes pineapples    // This line should match.
于 2017-03-01T14:22:57.003 に答える
-1

このコードでは、キーワードに文字、数字、アンダースコア、空白のみを使用できます。

set /p keyword="Enter keyword: " || Set keyword=

set keyword_replaced=%keyword: =_%

echo %keyword_replaced%| findstr /r "[^0-9a-zA-Z_]" > nul
if errorlevel 1 goto noexit
echo special characters in keyword not allowed (except space and _), TERMINATING
timeout 4
exit /b 0
:noexit
于 2015-04-30T11:19:24.970 に答える