正則表達式記事本++ (RegEx Notepad++)


問題描述

正則表達式記事本++ (RegEx Notepad++)

我有以下格式的一系列行:

               LINE:  5190 STNO:  22669  SI: VOICE
          CCT   LINE    STNO  SI    BUS TYPE
          003   6269                OPTI ONLY     
           MULTLINE 8. . . . . . . . . . . . . . .
           001 SUBUNIT . . . . . DIGITE MAIN      DEFIL/TRS
           (ALT_ROUT: N)       (OPTIIP  )
               LINE:  5291 STNO:  29956  SI: VOICE

我需要通過正則表達式(notepad++)找到“STNO:”之後的數字

大約有100這樣的比賽。

我厭倦了 STNO:\s+\d{4,5} 但它也將 STNO 帶入了我不想要的比賽。請幫忙。

我只需要保留匹配的結果,其餘的我想刪除匹配的項目或將匹配的項目複製到一個新文件中,以更容易為準。


參考解法

方法 1:

I suggest a two step approach. First get all the lines with the STNO and a number. Second remove everything except the number.

Select the Mark tab in the find dialogue. Ensure Bookmark line is ticked. In the Find what box enter STNO:\s*\d+ and then click Mark all.

Access Menu => Search => Bookmark => Copy bookmarked lines. Then paste into another buffer. Alternatively, to work in the same file use Menu => Search => Bookmark => Remove unmarked lines. Now you should have all the wanted lines in a buffer.

Do a regular expression search and replace setting Find what to be ^.*STNO:\s*(\d+).*$ and Replace with to \1. Then click Replace all.

The above assumes that there is only one number to be found per line.

=========================

As only the numbers are wanted, another method would be to put line breaks plus a marker around the wanted numbers, then delete any lines without the marker, finally delete the markers.

Let the marker be keep. Do a search and replace setting Find what to be keep and Replace with to a single space character, make sure Match case is not selected; then click Replace all. Next, do a regular expression search and replace setting Find what to be ^STNO:\s*(\d+) and Replace with to \r\nkeep\1\r\n. You might want Match case ticked; then click Replace all. Next do a mark lines (as described above) with Find what set to keep, followed by Menu => Search => Bookmark => Remove unmarked lines. Finally, do a search and replace setting Find what to be keep and Replace with to be empty.

(by ShahidAdrianHHH)

參考文件

  1. RegEx Notepad++ (CC BY‑SA 2.5/3.0/4.0)

#find #RegEx #replace #notepad++ #expression






相關問題

禁止查找和 grep“無法打開”輸出 (Suppress find & grep "cannot open" output)

Cakephp 複雜查詢 (Cakephp complex query)

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

Jquery 響應 find() 失敗? (Jquery respond to find() fail?)

正則表達式記事本++ (RegEx Notepad++)

使用 VBA 在 MS Word 表格中查找和替換可變長度文本 (Find & replace variable length text in MS Word tables using VBA)

獲取子字符串周圍的字符數半徑 (Get set number radius of characters around substring)

僅在不到一天的文件中搜索字符串 (Search for a string only in files less than a day old)

c++ 查找和替換整個單詞 (c++ Find and replace whole word)

使用 grep 過濾後如何忽略行首和行的一部分 (How to ignore beginning of line and parts of a line after filtering with grep)

查找函數加上循環給出 1004 錯誤 (Find function couple with loop gives 1004 error)

VS Code 查找和替換:當我輸入 ctrl+h 時,有沒有辦法保留我以前的查找項? (VS Code find-and-replace: is there a way to keep my previous find term when I type ctrl+h?)







留言討論