<?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[How to group lines with same beginning]]></title><description><![CDATA[<p dir="auto">Hello!<br />
I would like to know if it’s possible to group several lines that share the same beginning up to a certain mark into just one line.</p>
<p dir="auto">My file is like this:</p>
<pre><code>aaaa$$$$$$hello1
aaaa$$$$hello2
aaaa$$$$hello3
bbbb$$$$$$$$hello4
bbbb$$$$hello5
cccc$$$$hello6
cccc$$$$hello7
cccc$$$$hello8
</code></pre>
<p dir="auto">*aaaa, bbbb, cccc and hello1…hello8 are just examples, not the real text in my document.</p>
<p dir="auto">So, I would like to know how to group easily, maybe with a macro or a built-in feature in Notepad, these lines that share the same beginning (aaaa, bbbb, cccc) up to the mark $, resulting into:</p>
<pre><code>aaaa$$$$$$hello1 / hello2 / hello3
bbbb$$$$$$$$hello4 / hello5
cccc$$$$hello6 / hello7 / hello8
</code></pre>
<p dir="auto">Thinking of macros, I know how to record a simple macro, but I don’t know how to create a macro that basically goes through all the lines in the document from top to bottom and groups them with a function “If beginning of the line until you find a $ is the same as the beginning of the previous line, group this line with the previous one”.</p>
<p dir="auto">Could you please help me to do this?</p>
<p dir="auto">Thank you very much in advance.</p>
<p dir="auto">-–</p>
<p dir="auto"><em>moderator added code markdown around text; please don’t forget to <a href="https://community.notepad-plus-plus.org/topic/21925/faq-desk-formatting-forum-posts">use the <code>&lt;/&gt;</code> button to mark example text as “code”</a> so that characters don’t get changed by the forum</em></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/26089/how-to-group-lines-with-same-beginning</link><generator>RSS for Node</generator><lastBuildDate>Sun, 17 May 2026 05:16:27 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/26089.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Sep 2024 18:18:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to group lines with same beginning on Wed, 11 Sep 2024 14:28:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> said in <a href="/post/96535">How to group lines with same beginning</a>:</p>
<blockquote>
<p dir="auto">Python’s re uses a different engine than Notepad++ does. While this can be a “good thing”, sometimes it will trip a user up – they’ll get a “tricky” regular expression working in Notepad++, and then run into trouble when trying to automate using the same expression in a script.</p>
</blockquote>
<p dir="auto">This does in fact happen in multiple places in my script. I’ll just break down how the regular expressions I used had to change to be compatible with <a href="https://docs.python.org/3/library/re.html" rel="nofollow ugc">Python’s <code>re</code> engine</a>.</p>
<p dir="auto"><strong>STEP 1 REGEX CHANGES</strong></p>
<p dir="auto"><code>(?-s)^([^\$\r\n]*)(.*\R)(?:\1(.*)(?:\R|\z))*</code><br />
becomes<br />
<code>(?m)^([^$\r\n]*)([^\r\n]*(?:\r?\n|\r))((?:\1(?:[^\r\n]*)(?:\r?\n|\r)?)*)</code></p>
<ol>
<li><code>(?-s)</code> is unnecessary (because <code>.</code> already does not match newline by default in <code>re</code>)</li>
<li><code>(?m)</code> is necessary to make it so that <code>^</code> matches at the beginning of the file <em>and</em> at the beginning of lines. In Notepad++ regex, <code>^</code> matches the beginning of lines by default.</li>
<li>Every instance of <code>.</code> must become <code>[^\r\n]</code> because in Python <code>.</code> matches <code>\r</code>, which is bad because that is the first character of the <code>\r\n</code> sequence that indicates a newline in Windows.</li>
<li>Every instance of <code>\R</code> (shorthand for any newline) must become <code>(?:\r?\n|\r)</code>, which matches the three most common newlines (<code>\n</code>, <code>\r\n</code>, and <code>\r</code>)</li>
</ol>
<p dir="auto">The Step 2 regex also needs to be changed from <code>(?-s)(\R?)(\x07)?([^\$]*)(\$+)(.*)</code> to <code>((?:\r?\n|\r)?)(\x07)?([^$\r\n]*)(\$+)([^\r\n]*)</code> because of point 3 above (the lack of <code>\R</code> in Python’s <code>re</code>)</p>
<p dir="auto">Finally, I had to create callback functions (the <code>def replacer1(m):</code> and <code>def replacer2(m)</code>) above, because the replacement regexes I used in Notepad++ don’t work in Python.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96539</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96539</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Wed, 11 Sep 2024 14:28:11 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Wed, 11 Sep 2024 10:25:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a> said:</p>
<blockquote>
<p dir="auto">And before someone complains that this is basically just a couple of simple PythonScript commands wrapped around a pure Python script</p>
</blockquote>
<p dir="auto">It’s no problem.  In fact, it’s totally appropriate.  :-)</p>
<hr />
<p dir="auto">Just to point out:</p>
<p dir="auto">Python’s <code>re</code> uses a different engine than Notepad++ does.  While this can be a “good thing”, sometimes it will trip a user up – they’ll get a “tricky” regular expression working in Notepad++, and then run into trouble when trying to automate using the same expression in a script.  I’m not saying anything like that would happen with the specific problem of this thread…it’s just something to be aware of.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96535</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96535</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 11 Sep 2024 10:25:12 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Wed, 11 Sep 2024 06:19:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nataly-flower" aria-label="Profile: Nataly-Flower">@<bdi>Nataly-Flower</bdi></a></p>
<p dir="auto">Fortunately, this problem can still be solved with regular expressions, because the regex engines used by most scripting languages (including Python and C#) are not susceptible to the same match length limit that Notepad++ suffers from.</p>
<p dir="auto">Here’s a PythonScript script that would solve the problem. I’ve tested it with documents that have as many as 400000 consecutive lines that all have the same beginning before the first <code>$</code> character. And before someone complains that this is basically just a couple of simple PythonScript commands wrapped around a pure Python script, I’m aware of that and I just wanted to post this anyway.</p>
<p dir="auto">By the way, this highlights a general pattern: if you are dissatisfied with the performance of Notepad++'s native find/replace form, try using <code>re.sub</code> to do the same find/replace operation in PythonScript, and you will likely see a massive performance improvement.</p>
<pre><code>'''
====== SOURCE ======
Requires PythonScript (https://github.com/bruderstein/PythonScript/releases)
Based on this question: https://community.notepad-plus-plus.org/post/96456
====== DESCRIPTION ======
The goal of this script is to replace a document of the form
"""
aaaa$$$$$$hello1
aaaa$$$$hello2
aaaa$$$$hello3
bbbb$$$$$$$$hello4
bbbb$$$$hello5
cccc$$$$hello6
cccc$$$$hello7
cccc$$$$hello8
"""
with
"""
aaaa$$$$$$hello1 / hello2 / hello3
bbbb$$$$$$$$hello4 / hello5
cccc$$$$hello6 / hello7 / hello8
"""

In the words of the original poster, "So, I would like to know how to group easily, maybe with a macro or a built-in feature in Notepad, these lines that share the same beginning (aaaa, bbbb, cccc) up to the mark $"
====== EXAMPLE ======
See above.
'''
from Npp import editor
import re

# You could add a bunch of text that matches the problem with the below:
# editor.setText('\r\n'.join(('%s$$$$hello%d' % (x * 4, ii)) for x in 'abcdefghijklm' for ii in range(400_000)))

oldText = editor.getText()

# Use BEL to mark the first line in each series of lines with the same start
FIRST_REX = r'(?m)^([^$\r\n]*)([^\r\n]*(?:\r?\n|\r))((?:\1(?:[^\r\n]*)(?:\r?\n|\r)?)*)'
# print('======= DOING FIRST REPLACEMENTS ======')
def replacer1(m):
    # print(m.groups())
    return '\x07' + m.group(0)

newText1 = re.sub(FIRST_REX, replacer1, oldText)

# Convert your document into the desired final form
# print('======= DOING SECOND REPLACEMENTS ======')
def replacer2(m):
    grps = m.groups()
    # print(grps)
    return ('%s%s%s%s' % (grps[0], grps[2], grps[3], grps[4])) if grps[1] else (' / ' + grps[4])

newText2 = re.sub(r'((?:\r?\n|\r)?)(\x07)?([^$\r\n]*)(\$+)([^\r\n]*)', replacer2, newText1)
editor.setText(newText2)
</code></pre>
<p dir="auto"><strong>EDIT:</strong> The <a href="https://github.com/molsonkiko/JsonToolsNppPlugin/blob/main/docs/README.md#regex-search-form" rel="nofollow ugc">regex search form in JsonTools</a> can also achieve this task much faster than Notepad++, but still noticeably slower than PythonScript. The main advantage of the regex search form is that, when using the <code>s_fa</code> function, it provides a tree view making it easy to see all the capture groups of each regex search result.</p>
<p dir="auto">I have not tested other plugins like ColumnsPlusPlus or MultiReplace for this task, but in my experience neither of those plugins comes anywhere close to the raw performance of Python’s <code>re.sub</code> when the number of replacements is very large.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96530</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96530</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Wed, 11 Sep 2024 06:19:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Tue, 10 Sep 2024 17:13:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a></p>
<p dir="auto">Thank you very much for your assistance. I appreciate it a lot. :)</p>
<p dir="auto">I must say you have given me the key to realize what was happening. I was not really doing anything wrong, nor there is anything wrong with the regex formulated above by <a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a>, whom I also thank for his help once again.</p>
<p dir="auto">When you asked about the size of my file, I tried to perform the replacements in the original file and indeed it gives an error, and hovering the mouse over the little icon you said, I could see the following message: ‘Ran out of stack space trying to match the regular expression’. I must say it is noticeable here that I am still a novice in the use of Notepad++, because I didn’t know that a message could be seen in that icon, I thought it was an icon accompanying the Find: Invalid Regular Expression message.</p>
<p dir="auto">Therefore, I have tried again the replacements this time on a smaller version of the file, and this time they have worked perfectly, without problems. It was a question of file size.</p>
<p dir="auto">Having said this, I would like to thank you once again for helping me. Thank you so much.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96518</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96518</guid><dc:creator><![CDATA[Nataly Flower]]></dc:creator><pubDate>Tue, 10 Sep 2024 17:13:06 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Mon, 09 Sep 2024 19:46:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nataly-flower" aria-label="Profile: Nataly-Flower">@<bdi>Nataly-Flower</bdi></a> said in <a href="/post/96495">How to group lines with same beginning</a>:</p>
<blockquote>
<p dir="auto">after making sure that Search Mode is set to Regular expression and Wrap around is checked, when I try to do the replacement (I’ve tried to do it both by clicking “Replace All” and “Replace”), Notepad throws error message ‘Find: Invalid regular expression’.</p>
</blockquote>
<p dir="auto">Then you have done something wrong, because when I copy <a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a>’s first regex, and use it on your data, it works perfectly, without giving an error:</p>
<p dir="auto"><img src="/assets/uploads/files/1725908834023-dcdb199f-6b66-4c4d-9230-109e87e863e6-image.png" alt="dcdb199f-6b66-4c4d-9230-109e87e863e6-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">So does the second:<br />
<img src="/assets/uploads/files/1725909312795-d851302d-9a47-4f82-baea-60a1d6db32b8-image.png" alt="d851302d-9a47-4f82-baea-60a1d6db32b8-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">We cannot begin to guess what you did wrong, because you shared nothing about the error message: If you hover over the little popout-icon in the <code>Find: Invalid Regular Expression</code> message, it will tell you exactly what’s wrong with your regex.  Here’s one where I intentionally edited the regex to be invalid<br />
<img src="/assets/uploads/files/1725908999822-061b577f-f312-423b-8e45-92d4ec5465e8-image.png" alt="061b577f-f312-423b-8e45-92d4ec5465e8-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Hmm, I <em>said</em> you had done something wrong… but another question might be: how big is your file? And how many lines in a row might match with the exact same prefix?  Because  there is a limit to how many bytes can fit inside a capture group in a regex.  So if you have lines that are thousands of characters wide, or have thousands of the <code>aaaa</code>-prefixed lines, it might be enough to overwhelm the regex engine.  If it gets too big, it can come back with an <code>Invalid Regular Expression</code> message, even though it’s really an “invalid size” problem.  But you haven’t given us enough to be sure.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96500</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96500</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Mon, 09 Sep 2024 19:46:22 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Mon, 09 Sep 2024 18:59:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nataly-flower" aria-label="Profile: Nataly-Flower">@<bdi>Nataly-Flower</bdi></a></p>
<p dir="auto">It’s so similar to the other one that you missed a golden opportunity to learn something.  And since you were spoon fed, I’m confident you won’t be doing any learning.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96498</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96498</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Mon, 09 Sep 2024 18:59:54 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Mon, 09 Sep 2024 18:58:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a></p>
<p dir="auto">Thank you for the link, but although the question in there may be somewhat similar, I would prefer to learn through my own one, if possible, which is the one that matches exactly with my needs.</p>
<p dir="auto">You could consider this a fish of courtesy, and later, if I feel like eating similar fishes like this one, don’t worry I will do my best to catch them by myself. :)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96497</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96497</guid><dc:creator><![CDATA[Nataly Flower]]></dc:creator><pubDate>Mon, 09 Sep 2024 18:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Mon, 09 Sep 2024 18:50:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a></p>
<p dir="auto">Thank you so much, Mark, for your reply! :)</p>
<p dir="auto">I’m very sorry I couldn’t reply sooner. Due to several issues, I haven’t been able to. I apologize for it.</p>
<p dir="auto">Anyway, I really appreciate your help. I’ve tried to follow the steps you indicate, but at point 1, after making sure that Search Mode is set to Regular expression and Wrap around is checked, when I try to do the replacement (I’ve tried to do it both by clicking “Replace All” and “Replace”), Notepad throws error message ‘Find: Invalid regular expression’.</p>
<p dir="auto">What do you think we could change in the expression of that first Find what?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96495</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96495</guid><dc:creator><![CDATA[Nataly Flower]]></dc:creator><pubDate>Mon, 09 Sep 2024 18:50:40 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Sat, 07 Sep 2024 15:16:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mark-olson" aria-label="Profile: Mark-Olson">@<bdi>Mark-Olson</bdi></a></p>
<p dir="auto">I wanted to see if someone could learn to fish.<br />
Apparently you just wanted to feed them for today.<br />
:-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96461</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96461</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sat, 07 Sep 2024 15:16:49 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Fri, 06 Sep 2024 19:27:52 GMT]]></title><description><![CDATA[<p dir="auto">The following sequence of regex-replace should do the trick.</p>
<p dir="auto">Open the find/replace form with <code>Search-&gt;Replace...</code> or Ctrl+H (using default keybindings).</p>
<p dir="auto">Make sure <code>Search Mode</code> is set to <code>Regular expression</code> and <code>Wrap around</code> is checked.</p>
<ol>
<li>Mark the first line in each series of lines with the same start using the following replacement:
<ul>
<li><strong>Find what:</strong> <code>(?-s)^([^\$\r\n]*)(.*\R)(?:\1(.*)(?:\R|\z))*</code></li>
<li><strong>Replace with:</strong> <code>\x07${0}</code></li>
</ul>
</li>
<li>Convert your document into the desired final form (shown below)
<ul>
<li><strong>Find what:</strong> <code>(?-s)(\R?)(\x07)?([^\$]*)(\$+)(.*)</code></li>
<li><strong>Replace with:</strong> <code>(?2$1$3$4$5: / $5)</code></li>
</ul>
</li>
</ol>
<p dir="auto">Result:</p>
<pre><code>aaaa$$$$$$hello1 / hello2 / hello3
bbbb$$$$$$$$hello4 / hello5
cccc$$$$hello6 / hello7 / hello8
</code></pre>
<p dir="auto">The first and second regular expressions can be tweaked a fair amount to solve similar problems to this one.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96458</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96458</guid><dc:creator><![CDATA[Mark Olson]]></dc:creator><pubDate>Fri, 06 Sep 2024 19:27:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to group lines with same beginning on Fri, 06 Sep 2024 18:56:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nataly-flower" aria-label="Profile: Nataly-Flower">@<bdi>Nataly-Flower</bdi></a></p>
<p dir="auto">Your problem seems very similar to this one: <a href="https://community.notepad-plus-plus.org/topic/26062/eliminate-excess-verbiage-from-audio-transcript">https://community.notepad-plus-plus.org/topic/26062/eliminate-excess-verbiage-from-audio-transcript</a></p>
<p dir="auto">Maybe you can figure out how to do what you want, based on the solution of that other one.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/96457</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/96457</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Fri, 06 Sep 2024 18:56:29 GMT</pubDate></item></channel></rss>