問題描述
正則表達式記事本++ (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.