-1

I'm trying to remove lines with more than 3 words in Notepad++. I can't seem to find the expression for "word" on google, and I can't use the "number of characters" as it wouldn't be feasible.

5
  • 1
    Are you running this on Windows? What version? Or is it something else?
    – mdpc
    Apr 28, 2014 at 6:38
  • Are you looking specifically for a notepad++ solution, or will you accept other options? This task would not be difficult with a number of scripting languages.
    – MaQleod
    Apr 28, 2014 at 6:41
  • @mdpc I'm running it on Windows 7 64bit, notepad++ is v6.5.5. Apr 28, 2014 at 6:42
  • @MaQleod other options are fine as long as they aren't difficult. Apr 28, 2014 at 6:43
  • Here's how you could do it in PowerShell: gc in.txt | ? { ($_ -split "\s").Count -le 3 } | sc out.txt
    – dangph
    Apr 28, 2014 at 6:58

2 Answers 2

2

In Notepad++ you can do a search-replace with the following regular expression: Find what: ^\S+[ \t]+\S+[ \t]+\S+.*(\r\n)? Replace with: (nothing)

Un-check "Wrap around", search mode to "Regular expression".

Explained:

Leading ^ means beginning of line.

\S matches non-whitespace characters; therefore \S+ matches consecutive non-whitespace characters i.e. words.

[ \t]+ matches space(s) and tab(s) as word boundaries.

.* means any characters; we have three words in front of that already so we simply include everything until we reach a new line.

\r\n means new line for Windows files.

3
  • So where exactly does the (3) for the number of words go? Apr 28, 2014 at 7:19
  • For simplicity I hard coded the regular expression. You will see the pattern \S+ is repeated for 3 times, each of them separated by [ \t]+ pattern. If you wish to make it more flexible you can amend that portion with (\S+[ \t]+){2}\S+. {2} means the previous pattern enclosed in parenthesis repeated by two times, adding the final \S+ make it three.
    – Kenneth L
    Apr 28, 2014 at 7:43
  • 1
    From Markov: \r\n means new line for Windows files. If your file is encoded for example as UTF8 (maybe most common today), the line break would be \n only (not \r\n). Hence Kenneth's code should become: ^\S+[ \t]+\S+[ \t]+\S+.*(\n)
    – fixer1234
    Feb 5, 2018 at 9:19
0

May I contribute to this? As Kenneth said, \r\n means new line for Windows files. If your file is encoded for example as UTF8 (maybe most common today), the line break would be \n only (not \r\n). Hence Kenneth's code should become:

^\S+[ \t]+\S+[ \t]+\S+.*(\n)
3
  • This is not a good post. All your changes achieve is limiting the use of the other post.
    – Dave
    Feb 5, 2018 at 8:46
  • Answers are intended for solutions. Discussion of another answer should go in a comment. I went ahead and copied this to a comment for you. You might want to delete this post so it doesn't attract downvotes.
    – fixer1234
    Feb 5, 2018 at 9:21
  • Dave, wrong. My post extends the use of Kenneth's solution beyond windows-encoded files where Kenneth's code won't work.
    – Markov
    Feb 6, 2018 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .