替換 Line 但保留部分內容在 Notepad ++ 中

替換 Line 但保留部分內容在 Notepad ++ 中

我想使用 Notepad++ 尋找/替換大塊程式碼:

我有很多:

角度[]={6.2744589,5.4066987,1.4066987};

有 3 個值。我需要將所有第一個和第二個值都設為 0 並將值保留在第三個位置:S

角度[]={6.2744589,5.4066987,1.4066987};

角[]={0,0,1.4066987};

謝謝!

答案1

這可以透過 Notepad++ 中的 regex(正規表示式)搜尋和替換來完成。

尋找:angles\[\]={\d+\.\d+,\d+\.\d+,(\d+\.\d+)};

用。angles[]={0,0,\1}

請注意,在上面的表達式中,我們轉義了括號,因此存在反斜線 (),第三組是捕獲組(括號之間)。最後我們對第一組和第二組進行替換,第三組就是原始值(簡稱\1:捕獲值)。

我附上螢幕截圖範例。

Notepad++ 正規表示式替換

答案2

  • Ctrl+H
  • 找什麼:\bangles\[\]=\{\K[^,]+,[^,]+
  • 用。0,0
  • Replace all

解釋:

\b          : word boundary, to be sure to match angles but not somethingangles
angles      : literally angles
\[\]=\{     : literally []=, brackets have to be escaped as they have special meaning in regex
\K          : Forget all we have seen until this point
[^,]+       : 1 or more any character that is not a comma, that matches also negative values
,           : a comma
[^,]+       : 1 or more any character that is not a comma
  • 檢查正規表示式
  • 不要檢查. matches newline

給定範例的結果:

angles[]={0,0,1.4066987};

相關內容