2016-05-24 8 views
-1

入力テキストの上に、設定したパターンに一致するテキストを表示させようとしています。入力パターンと一致するテキストのテキストファイルを検索する

例えば、ユーザの入力「FastModeIdleImmediateCount "= DWORD値:00000000' の場合、私は[HKEY_CURRENT_CONFIG \システム\ CURRENTCONTROLSET \ Enumの\ SCSI \ディスク& Ven_ATA & Prod_TOSHIBA_MQ01ABD0 \ 4 & 6a0976b &である、上記最も近いHKEYを取得する必要がありますがこの場合の0 & 000000]。

[HKEY_CURRENT_CONFIG\System\CurrentControlSet\Enum\SCSI\Disk&Ven_ATA&Prod_TOSHIBA_MQ01ABD0\4&6a0976b&0&000000] 
"StandardModeIdleImmediateCount"=dword:00000000 
"FastModeIdleImmediateCount"=dword:00000000 

[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES] 

[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES\TSDDD] 

[HKEY_CURRENT_CONFIG\System\CurrentControlSet\SERVICES\TSDDD\DEVICE0] 
"Attach.ToDesktop"=dword:00000001 

誰もが、私はそのような何かをコーディングすることができますどのように私を示していただけますか?私は、ブラケットとテキストに一致する正規表現で遊んでみましたが、私はそれを作る方法がわからないです入力の上にあるテキストのみを検索します。

+0

詳しくは...テキストソースは何ですか? Windowsレジストリで検索を実行しますか?キーまたは値を一致させますか? – ElmoDev001

答えて

1

あなたのファイルが.txtファイルであると仮定していますが、おそらくそうではありません。しかし、論理は同じです。 これは難しいことではありません。単純なfor()ループがこのトリックを行います。必要な記述を持つ コード:

string[] lines = File.ReadAllLines(@"d:\test.txt");//replace your directory. We're getting all lines from a text file. 

     string inputToSearchFor = "\"FastModeIdleImmediateCount\"=dword:00000000"; //that's the string to search for 

     int indexOfMatchingLine = Array.FindIndex(lines, line => line == inputToSearchFor); //getting the index of the line, which equals the matchcode 

     string nearestHotKey = String.Empty; 
     for(int i = indexOfMatchingLine; i >=0; i--) //looping for lines above the matched one to find the hotkey 
     {    
      if(lines[i].IndexOf("[HKEY_") == 0)   //if we find a line which begins with "[HKEY_" (that means it's a hotkey, right?) 
      { 
       nearestHotKey = lines[i];    //we get the line into our hotkey string 
       break;         //breaking the loop 
      } 
     } 

     if(nearestHotKey != String.Empty)    //we have actually found a hotkey, so our string is not empty 
     { 
      //add code... 
     } 
0

あなたは、その後、行にテキストを分割しようとするあなたのテキストを含む行のインデックスを見つける(完全一致または正規表現が使用されているかどうかは関係ありません)と可能性がありbacksearch最初のキーのために。最初に行を逆ソートすると役立ちます。

関連する問題