Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a regular expression for all lines followed by an empty line?

Now, in the snippet below, you will notice that there is a list of file paths. Each section (preceded and followed by empty lines) represents a set of files that are duplicated. I would like to select all but one of these so that I can then inverse the selection and use the list for a batch delete operation. I have no idea on how to construct a regular expression for selecting the same though.

The regular expression would have to select all lines excluding one in each section. How would I write such an expression?

  "H:\S\recup_dir.17\f171236272.mp3"
  "H:\S\recup_dir.8\f37197984.mp3"

  "H:\S\recup_dir.16\f168470040.gz"
  "H:\S\recup_dir.17\f170038264.gz"

  "H:\S\recup_dir.18\f218780056.mp3"
  "H:\S\recup_dir.9\f46500864.mp3"

  "H:\S\recup_dir.1\f0712280.mp3"
  "H:\S\recup_dir.20\f330856896.mp3"

  "H:\S\recup_dir.20\f304869798.mp3"
  "H:\S\recup_dir.9\f50157574.mp3"

  "H:\S\recup_dir.19\f246711560.mp3"
  "H:\S\recup_dir.9\f49831408.mp3"

  "H:\S\recup_dir.14\f108337512.mp3"
  "H:\S\recup_dir.2\f8089064.mp3"

  "H:\S\recup_dir.16\f152853840.mp3"
  "H:\S\recup_dir.20\f315839720.mp3"
  "H:\S\recup_dir.8\f38617728.mp3"

  "H:\S\recup_dir.22\f414604664.mp3"
  "H:\S\recup_dir.8\f38191280.mp3"

  "H:\S\recup_dir.10\f52349328.mp3"
  "H:\S\recup_dir.22\f415832696.mp3"

  "H:\S\recup_dir.17\f171137807.mp3"
  "H:\S\recup_dir.20\f303943983.mp3"
  "H:\S\recup_dir.8\f37378127.mp3"

  "H:\S\recup_dir.15\f123439872.mp3"
  "H:\S\recup_dir.2\f7974272.mp3"

  "H:\S\recup_dir.2\f6994232.mp3"
  "H:\S\recup_dir.20\f281329576.mp3"
like image 583
Siddharth Avatar asked Oct 21 '22 17:10

Siddharth


1 Answers

You can use this regex:

(?<!\n\n)(?<!\A)^ *\S+

regex101 demo.

The initial regex (?<!\n\n|\A)^\S+ didn't work because of the variable width lookbehind I think, but I tested it on portable SublimeText 2.0.2. Might be something which works on v3.X.

(EDIT: Had to add extra spaces in regex)

like image 68
Jerry Avatar answered Nov 15 '22 10:11

Jerry