notepad++ deleting all text except what matches re

2019-02-21 17:55发布

问题:

I have a text file with information about a recording, including track numbers and names, and I want to delete all the other text except the track number and track names. For example the text file looks like this:

text text text text
text text text text text text text text
text text text text text text text text text text text text
text text text text
text text text text text text text text
text text text text 
text text text text text text text text text text text text text text text text 
text text text text text text text text 
text text text text 

d1t01 - trackname

d1t02 - trackname

d1t03 - trackname

d1t04 - trackname

d1t05 - trackname

d1t06 - trackname

d1t07 - trackname

d1t08 - trackname

d1t09 - trackname

d1t10 - trackname

d1t11 - trackname

text text text text
text text text text text text text text
text text text text text text text text text text text text
text text text text
text text text text text text text text
text text text text 
text text text text text text text text text text text text text text text text 
text text text text text text text text 
text text text text 

I want to be left with just this:

d1t01 - trackname

d1t02 - trackname

d1t03 - trackname

d1t04 - trackname

d1t05 - trackname

d1t06 - trackname

d1t07 - trackname

d1t08 - trackname

d1t09 - trackname

d1t10 - trackname

d1t11 - trackname

I figured out how to match the beginning of the line with this regex:

d[0-9]+t[0-9]+[0-9]+ -

But I can't figure out how to delete the rest of the "text" and only leave those lines. I also need it to delete the "returns" and not be left with any blank lines in the text file.

Thank you!

回答1:

EDIT: You should be able to use the following here.

Find: ^(?!d\w+).*\r?\n?
Replace:   

Regular Expression:

^              the beginning of the string
(?!            look ahead to see if there is not:
  d              'd'
  \w+            word characters (a-z, A-Z, 0-9, _) (1 or more times)
)              end of look-ahead
.*             any character except \n (0 or more times)
\r?            '\r' (carriage return) (optional)
\n?            '\n' (newline) (optional)


回答2:

hwnd's answer is close, but it won't remove all the new lines. You said you don't want any blank lines in the file at all, so this would be a more complete solution:

Find: ^(?!d[0-9]+t[0-9]+).*\r?\n?

Replace:

In this case, your initial body of text would be replaced by

d1t01 - trackname
d1t02 - trackname
d1t03 - trackname
d1t04 - trackname
d1t05 - trackname
d1t06 - trackname
d1t07 - trackname
d1t08 - trackname
d1t09 - trackname
d1t10 - trackname
d1t11 - trackname


回答3:

Step 1 - Match lines. Find dialog > Mark tab, you can bookmark lines that match.

Step 2 - Remove lines bookmarked OR Remove lines not bookmarked. Search > Bookmark > Remove Unmarked Lines or Remove Bookmarked lines