Search code examples
regexsublimetext3

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


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.


Solution

  • 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