Regex - How to delete the first 10 lines / and the last 10 lines

5.8k views Asked by At

I'm trying to make a regex that deletes the first and the last 10 lines of my 200 txt files with "Search and Replace all"

I tried (\s*^(\h*\S.*)){10} to delete the first 10 lines blanks included but it doesn't work well.

2

There are 2 answers

0
Wiktor Stribiżew On BEST ANSWER

In Notepad++, you will need to match the whole document text but only capure lines from 11 to the end.

Find: \A.*(?:\R.*){9}\R?([\s\S]*)
Replace: $1

To remove the last 10 lines:

Find: ^.*(?:\R.*){9}\z
Replace: empty

Details:

  • \A - start of the document
  • .* - any zero or more chars other than line break symbols (the whole first line)
  • (?:\R.*){9} - 9 sequences of a line break (\R) followed with any 0+ chars other than line break chars
  • \R? - an optional line break (if there are just 10 lines)
  • ([\s\S]*) - Group 1 capturing the rest of the document.

The second regex:

  • ^ - start of a line
  • .* - the line itself up to the line break
  • (?:\R.*){9} - 9 lines: line break + any 0+ chars other than line break chars
  • \z - the end of the document.

$1 is the backreference to the text stored in Group 1.

The result of hitting a Replace All button on a 20+ line document to remove the first 10 lines:

enter image description here

And the last 10 lines:

enter image description here

0
AudioBubble On

Just a simple Replace All using this

(?m)(?:\A(?>^.*\R?){10}|(?>^.*\R?){10}\z)

Expanded

 (?m)
 (?:
      \A
      (?> ^ .* \R? ){10}
   |  (?> ^ .* \R? ){10}
      \z
 )

If it doesn't support the \R syntax, substitute

(?>\r\n|\n|\r|\f|\x0b|\x85) - Ascii
(?>\r\n|\n|\r|\f|\x0b|\x85|\x{2028}|\x{2029}) - Unicode