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.
2 Answers
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.
-
-
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. Apr 28, 2014 at 7:43 -
1
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)
-
This is not a good post. All your changes achieve is limiting the use of the other post.– DaveFeb 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. 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.– MarkovFeb 6, 2018 at 10:11
gc in.txt | ? { ($_ -split "\s").Count -le 3 } | sc out.txt