Sublime & Regex: How to find all lines that are 3 letters or less, excluding certain lines?

Viewed 88

I have a document that is 10,000 lines long. I would like to remove all lines that are 3 letters or less, excluding any lines that start with a § symbol or excluding any lines that are all in caps.

Example: Before removal:

§day
DOG
Happy
Monday
Now
Yes
Sunday
§new day.txt
DIY
Leg
Books
Car
Home

After removal:

§day
DOG
Happy
Monday
Sunday
§new day.txt
DIY
Books
Home

DOG & DIY are not affected as they are all capitals. The lines start with § are also not affected.


My attempts

I know that this code can be used to make Regex ignore all lines that are in capitals and all lines that start with a § (In the example, the code is searching for many or north or one).

(^(?:(?:§.*|[^[:alpha:]\n\r]*[[:upper:]]+(?:[^[:alpha:]\n\r]+[[:upper:]]+)*[^[:alpha:]\n\r]*))$|(?i:\b(?:many|north|one)\s+of\b))|(?i:\bof\b)

I also know that this code can be used to find all words that are 3 letters or less

'^.{1,3}$'

Is there any way I can combine them?

I tried replacing many|north|one with '^.{1,3}$' but it didn’t work.

2 Answers

You can search using this regex:

^(?!(?:§|[A-Z]+$)).{0,3}(?:[\r\n]+|\z)

and replace using empty string:

Make sure mode m or MULTILINE is enabled in your regex.

RegEx Demo

RegEx Details:

  • ^: Start
  • (?!(?:§|[A-Z]+$)): Negative lookahead to assert a failure if line starts with § or contains only uppercase letters
  • .{0,3}(?:[\r\n]+|\z): Match any character 0 to 3 times followed by a 1 or more line breaks or end of file

Before:

enter image description here

After

enter image description here

I suggest using

(?-i)^(?!§|[A-Z]+$).{1,3}$\R?

See the regex demo. Details:

  • (?-i) - turn on case sensitivity (or, you may omit it and turn the Aa option on as shown in the screenshot below)
  • ^ - start of a line
  • (?!§|[A-Z]+$) - no § at the start and a line only made of uppercase ASCII letters are allowed
  • .{1,3} - 1 to 3 chars
  • $ - end of a line
  • \R? - an optional line break sequence.

SublimeText3 test:

enter image description here

        V

enter image description here

Related