<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[I want to sort text phrases by their number of words]]></title><description><![CDATA[<p dir="auto">I would like the program to be able to select or identify a sentence automatically (from one point to the next or from one point to the question mark or to the exclamation mark) and to order each sentence one by the number of words that each one has. So that the sentence with fewer words would be at one end and the one with more words at the other.</p>
<p dir="auto">For exemple:</p>
<p dir="auto">Normal text:<br />
The day was very difficult today. I hope the next few days get better. Do you hope the same? I hope you hope the same as me!</p>
<p dir="auto">Sorted text:</p>
<ol>
<li>Do you hope the same?</li>
<li>The day was very difficult today.</li>
<li>I hope the next few days get better.</li>
<li>I hope you hope the same as me!</li>
</ol>
<p dir="auto">it could also be:<br />
Do you hope the same? The day was very difficult today.  I hope the next few days get better. I hope you hope the same as me!</p>
<p dir="auto">I don’t know how to do this, can someone help me?</p>
<p dir="auto">Thanks in advance</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/19365/i-want-to-sort-text-phrases-by-their-number-of-words</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 16:11:49 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/19365.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 May 2020 19:49:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to I want to sort text phrases by their number of words on Thu, 07 May 2020 00:52:37 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/thunderdog" aria-label="Profile: thunderdog">@<bdi>thunderdog</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a> and <strong>All</strong>,</p>
<p dir="auto"><strong>Peter</strong>, nice <strong>shot</strong> , too ! As a <strong>sort</strong> was necessary, I simply thought about a <strong>second</strong> regex S/R. But you’re <strong>right</strong>, we can perfectly use a <strong>composite</strong> regex to get <strong>all</strong> the job done ;-))</p>
<p dir="auto">So, the road map is :</p>
<ul>
<li>
<p dir="auto">Perform the regex S/R, <strong>twice</strong></p>
</li>
<li>
<p dir="auto">Run the alphabetic ( Unicode ) ascending <strong>sort</strong></p>
</li>
<li>
<p dir="auto">Perform the <strong>same</strong> regex S/R, <strong>once</strong> again</p>
</li>
</ul>
<p dir="auto">Note that I also <strong>slightly</strong> shorten your search regex ! Here is the <strong>final</strong> solution :</p>
<hr />
<p dir="auto">Assuming the <strong>initial</strong> text of <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a>, <strong>normalized</strong> in <strong><code>two</code></strong> lines, which does <strong>end</strong> with either a <strong>dot</strong>, an <strong>exclamation mark</strong> or an <strong>question mark</strong> !</p>
<pre><code class="language-diff">The day was very difficult today. I hope the next few days get better. Do you hope the same? I hope you hope the same as me!
  Hello, world! This sentence is just ten teeny-tiny words long, silly goose! No?
</code></pre>
<p dir="auto">SEARCH <strong><code>(?-s)^#+\t(.+)#$|\w+[^\w\r\n]*(?=.*\t)|\h*(.+?[.?!])(?!#)</code></strong></p>
<p dir="auto">REPLACE <strong><code>?2\2\t\2#\r\n:?1\1:#</code></strong></p>
<p dir="auto">After <strong><code>two</code></strong> clicks on the <strong><code>Replace All</code></strong> button, we obtain :</p>
<pre><code class="language-diff">######	The day was very difficult today.#
########	I hope the next few days get better.#
#####	Do you hope the same?#
########	I hope you hope the same as me!#

##	Hello, world!#
###########	This sentence is just ten teeny-tiny words long, silly goose!#
#	No?#
</code></pre>
<p dir="auto">Now, we click on the <strong><code>Edit &gt; Line Operations &gt; Sort Lines Lexicographically Ascending</code></strong> option and get :</p>
<pre><code class="language-diff">
#	No?#
##	Hello, world!#
#####	Do you hope the same?#
######	The day was very difficult today.#
########	I hope the next few days get better.#
########	I hope you hope the same as me!#
###########	This sentence is just ten teeny-tiny words long, silly goose!#
</code></pre>
<p dir="auto">Let’s go back to fhe <strong>Replace</strong> dialog , again and use the <strong>same</strong> S/R</p>
<p dir="auto">SEARCH <strong><code>(?-s)^#+\t(.+)#$|\w+[^\w\r\n]*(?=.*\t)|\h*(.+?[.?!])(?!#)</code></strong></p>
<p dir="auto">REPLACE <strong><code>?2\2\t\2#\r\n:?1\1:#</code></strong></p>
<p dir="auto">After <strong><code>one</code></strong> <strong>final</strong> click on the <strong><code>Replace All</code></strong> button, here is your <strong>expected</strong> text :</p>
<pre><code class="language-diff">
No?
Hello, world!
Do you hope the same?
The day was very difficult today.
I hope the next few days get better.
I hope you hope the same as me!
This sentence is just ten teeny-tiny words long, silly goose!
</code></pre>
<p dir="auto">Wow… Awesome !</p>
<p dir="auto">And if we want to get <strong>all</strong> that text in a <strong>single</strong> paragraph, just apply this <strong>last</strong> regex S/R !</p>
<p dir="auto">SEARCH <strong><code>^\R|(\R)</code></strong></p>
<p dir="auto">REPLACE <strong><code>?1\x20</code></strong></p>
<p dir="auto">giving :</p>
<pre><code class="language-diff">No? Hello, world! Do you hope the same? The day was very difficult today. I hope the next few days get better. I hope you hope the same as me! This sentence is just ten teeny-tiny words long, silly goose!
</code></pre>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/53568</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/53568</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Thu, 07 May 2020 00:52:37 GMT</pubDate></item><item><title><![CDATA[Reply to I want to sort text phrases by their number of words on Wed, 06 May 2020 23:51:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> said in <a href="/post/53564">I want to sort text phrases by their number of words</a>:</p>
<blockquote>
<p dir="auto">…</p>
</blockquote>
<p dir="auto">Wow.  I take back “You cannot do it natively in Notepad++”, because yours fits the original spec without the quantity assumption that I made. Great job!</p>
<p dir="auto">and <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a>’s</p>
<blockquote>
<p dir="auto"><code>[.?!]</code></p>
</blockquote>
<p dir="auto">vs my</p>
<blockquote>
<p dir="auto"><code>[\?\!\.]</code></p>
</blockquote>
<p dir="auto">As I said recently, I tend to over-escape characters.  I thought I had improved, but… there you go. ;-(</p>
<blockquote>
<p dir="auto">Next time, we can dissect this regular expression and see how it works</p>
</blockquote>
<p dir="auto">I won’t give spoilers to the OP… but to figure out what was really going on, I had to do it with a bunch of single replace-once clicks, so I could see.</p>
<p dir="auto">I really like that it works without restrictions on quantity.</p>
<p dir="auto">And, to prove that I understood it, I got it down to a single regex with three replace all clicks, by using a second alternation and a second capture group in the search, and another conditional in the replace:<br />
SEARCH <code>(?-s)^(?=#)(?:#+\t(.*)#$)|\w+[^\w\r\n]*(?=.*\t)|\h*(.+?[.?!])(?!#)</code><br />
REPLACE <code>?2\2\t\2#\r\n:?1\1:#</code></p>
<p dir="auto">That was fun. :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/53567</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/53567</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 06 May 2020 23:51:23 GMT</pubDate></item><item><title><![CDATA[Reply to I want to sort text phrases by their number of words on Wed, 06 May 2020 23:30:06 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/thunderdog" aria-label="Profile: thunderdog">@<bdi>thunderdog</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">If we assume that :</p>
<ul>
<li>
<p dir="auto">A sentence begins to the <strong>first non-blank</strong> character till, either, a <strong>dot</strong>, a <strong>question mark</strong> or an <strong>exclamation mark</strong></p>
</li>
<li>
<p dir="auto">The <strong>last</strong> sentence of a line also <strong>ends</strong> with a <strong>dot</strong>, a <strong>question mark</strong> or an <strong>exclamation mark</strong></p>
</li>
<li>
<p dir="auto">Your text does <strong>not</strong> contain any <strong>tabulation</strong> ( <strong><code>\t</code></strong> ) char NOR the <strong>sharp</strong> <strong><code>#</code></strong> character</p>
</li>
</ul>
<p dir="auto">Here is my <strong>regex</strong> solution !</p>
<hr />
<p dir="auto">Taking your example, below, as the <strong>input</strong> text :</p>
<pre><code class="language-diff">The day was very difficult, today. I hope : the next few days get better. Do you hope the same? I hope you hope the same; as me!
</code></pre>
<p dir="auto">Then, the following regex S/R, after <strong>TWO successive</strong> clicks on the <strong><code>Replace All</code></strong> button, with the <strong><code>Regular expression</code></strong> option <strong>ticked</strong> :</p>
<p dir="auto">SEARCH <strong><code>(?-s)\w+[^\w\r\n]*(?=.*\t)|\h*(.+?[.?!])(?!#)</code></strong></p>
<p dir="auto">REPLACE <strong><code>?1\1\t\1#\r\n:#</code></strong></p>
<p dir="auto">will change your text into :</p>
<pre><code class="language-diff">######	The day was very difficult, today.#
########	I hope : the next few days get better.#
#####	Do you hope the same?#
########	I hope you hope the same; as me!#
</code></pre>
<p dir="auto">After a <strong>classical</strong> sort ( <strong><code>Edit &gt; Line Operations &gt; Sort lines lexicographically ascending</code></strong> ), you get :</p>
<pre><code class="language-diff">#####	Do you hope the same?#
######	The day was very difficult, today.#
########	I hope : the next few days get better.#
########	I hope you hope the same; as me!#
</code></pre>
<p dir="auto">Finally, the simple regex S/R, below, get your <strong>expected</strong> text !</p>
<p dir="auto">SEARCH <strong><code>#+|\t</code></strong></p>
<p dir="auto">REPLACE <strong><code>Leave EMPTY</code></strong></p>
<pre><code class="language-diff">Do you hope the same?
The day was very difficult, today.
I hope : the next few days get better.
I hope you hope the same; as me!
</code></pre>
<hr />
<p dir="auto">I guess you’ve already grasped the <strong>concept</strong> ! Next time, we can <strong>dissect</strong> this <strong>regular expression</strong> and see <strong>how</strong> it works ;-))</p>
<p dir="auto">Note, also, that the <strong>two working</strong> characters ( <strong><code>\t</code></strong> and <strong><code>#</code></strong> ) can be changed, <strong>independently</strong>, if necessary.</p>
<p dir="auto">Ah, also, regarding the <strong>first</strong> regex S/R, I made <strong>sure</strong> that a <strong><code>3rd</code></strong> press, on the <strong><code>Replace All</code></strong> button, will <strong>not</strong> find any more occurrences !</p>
<p dir="auto">See you later !</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/53564</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/53564</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Wed, 06 May 2020 23:30:06 GMT</pubDate></item><item><title><![CDATA[Reply to I want to sort text phrases by their number of words on Wed, 06 May 2020 21:39:06 GMT]]></title><description><![CDATA[<p dir="auto">You cannot do it natively in Notepad++, with only builtin tools, in the truly generic situation you described.  I cannot think that it would be a general-purpose enough sequence of events that any text editor includes it natively or even as a plugin.</p>
<p dir="auto">The two biggest problems:</p>
<ol>
<li>Defining beginning/end of a sentence reasonably:  You can find periods with regular expressions (regex).  But unfortunately, while periods <em>can</em> be used to end sentences, they can <em>also</em> be used for abbreviations both before and in between letters (Dr. vs M.D.), and many other places that are not the end of the sentence.  Unlike when I was a kid, in modern text files, you cannot rely on “period space space” being unambiguously “end of sentence”, since many text files and style guides use single spaces after sentence-ending periods.  And don’t get me started on parsing end-of-sentence when quotations are involved.  A well-trained A.I. (or a reasonably-educated human) can <em>usually</em> get it right, but there isn’t a reasonable regex out there which will incorporate all the nuances.  (The author said, “I hope Dr. Bob lives on Private Dr. in the same town where Harry Potter lives.” The programmer asked, “Can you see why this would be hard to parse in regex?”)</li>
<li>Counting words in a group of text is not a feature built into Notepad++.</li>
</ol>
<p dir="auto">So, even if you make simplistic assumptions and can get a regex to parse all of <em>your</em> sentences, you still cannot count words in each of those sentences without invoking a programming language – at which point it becomes mostly a programming challenge.</p>
<p dir="auto">This is not a code-writing service.  This is not a general programming forum for asking programming questions.  So from that perspective, it’s not on-topic for a Notepad++ forum.</p>
<p dir="auto">However, I had an idea while writing this up of a way to do it fully inside Notepad++, given some limiting assumptions:</p>
<ol>
<li>A period, question mark, or exclamation point are the only valid sentence enders.</li>
<li>Those three characters are only at the end of a sentence and never inside a sentence (so no abbreviations).</li>
<li>A “word” is defined as any sequence of non-whitespace characters (so “code-writing” is one word)</li>
<li>There are no quotations.</li>
<li>No sentence will have more than 10 words</li>
</ol>
<p dir="auto">Sequence:</p>
<ol>
<li>Edit &gt; Select All (^A)</li>
<li>Edit &gt; Line Operations &gt; Join (^J)</li>
<li>Search &gt; Replace<br />
<strong>Find What</strong>: <code>(?&lt;=[\?\!\.])\s*</code><br />
<strong>Replace With</strong>: <code>\r\n</code><br />
<strong>Search Mode</strong> = <code>regular expression</code> (assumed from here on out)<br />
Hit <strong>Replace All</strong></li>
<li>Search &gt; Replace<br />
<strong>Find What</strong>: <code>^(\S+\h*)((?1))?((?1))?((?1))?((?1))?((?1))?((?1))?((?1))?((?1))?((?1))?$</code><br />
<strong>Replace With</strong>: <code>(?{10}10\::(?{9}09\::(?{8}08\::(?{7}07\::(?{6}06\::(?{5}05\::(?{4}04\::(?{3}03\::(?{2}02\::(?{1}01\::()))))))))))$0</code></li>
<li>Edit &gt; Line Operations &gt; Sort as Integers Ascending</li>
<li>Search &gt; Replace<br />
<strong>Find What</strong>: <code>^\d{2}:</code><br />
<strong>Replace With</strong>: empty string</li>
<li>Edit &gt; Select All (^A)</li>
<li>Edit &gt; Line Operations &gt; Join (^J)</li>
</ol>
<p dir="auto">START =</p>
<pre><code class="language-x">The day was very difficult today. I hope the next few days get better. Do you hope the same? I 
hope you hope the same as me! Hello, world! This sentence is just ten teeny-tiny words long, 
silly goose! No?
</code></pre>
<p dir="auto">After #3 =</p>
<pre><code class="language-z">The day was very difficult today.
I hope the next few days get better.
Do you hope the same?
I hope you hope the same as me!
Hello, world!
This sentence is just ten teeny-tiny words long, silly goose!
No?
</code></pre>
<p dir="auto">After #4 =</p>
<pre><code class="language-z">06:The day was very difficult today.
08:I hope the next few days get better.
05:Do you hope the same?
08:I hope you hope the same as me!
02:Hello, world!
10:This sentence is just ten teeny-tiny words long, silly goose!
01:No?
</code></pre>
<p dir="auto">after #5 =</p>
<pre><code class="language-z">
01:No?
02:Hello, world!
05:Do you hope the same?
06:The day was very difficult today.
08:I hope the next few days get better.
08:I hope you hope the same as me!
10:This sentence is just ten teeny-tiny words long, silly goose!
</code></pre>
<p dir="auto">after #6 =</p>
<pre><code class="language-z">
No?
Hello, world!
Do you hope the same?
The day was very difficult today.
I hope the next few days get better.
I hope you hope the same as me!
This sentence is just ten teeny-tiny words long, silly goose!
</code></pre>
<p dir="auto">after #8 =</p>
<pre><code class="language-z"> No? Hello, world! Do you hope the same? The day was very difficult today. I hope the next few days get better. I hope you hope the same as me! This sentence is just ten teeny-tiny words long, silly goose!
</code></pre>
<p dir="auto">--------------------</p>
<p dir="auto">Quickie explanation of the regex in #4: look for 1 to 10 “words” that are made up of non-space followed by zero or more space separators.   In the replacement, use the conditional replacement: if it found #10, prefix with <code>10:</code>, else <code>09:</code>, else <code>08:</code>, … else <code>01:</code> else if it doesn’t match, don’t prefix at all.</p>
<p dir="auto">If you wanted to match more than 10 “words”, you’d have to have more matches in the FIND, and more nested conditionals in the REPLACE.  I went to 10 as a proof of concept.  (I’m sure yours goes to 11.)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/53562</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/53562</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 06 May 2020 21:39:06 GMT</pubDate></item></channel></rss>