Shahid
Shahid

Reputation: 65

RegEx Notepad++

I have series of line in following format:

               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

What I need to find through regex (notepad++) are the numbers just after "STNO:"

There are approximately 100 of such matches.

I tired STNO:\s+\d{4,5} but it is taking STNO also into the match which i do not want. Please help.

I need to keep the matched result only and rest i want to delete or copy the matched items to a new file whichever is easier.

Upvotes: 0

Views: 217

Answers (1)

AdrianHHH
AdrianHHH

Reputation: 14056

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.

Upvotes: 1

Related Questions