<?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 use RegEx to split 5000 characters but preserving sentense?]]></title><description><![CDATA[<p dir="auto">I have articles inside txt files.</p>
<p dir="auto">I wish to split each text file by 5000 characters, but preserving full sentence by the last period.</p>
<p dir="auto">How to identify the 1st match, 2nd match?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/19738/how-to-use-regex-to-split-5000-characters-but-preserving-sentense</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 17:58:26 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/19738.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 20 Jul 2020 08:28:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Wed, 11 May 2022 01:10:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/brent-parker" aria-label="Profile: brent-parker">@<bdi>brent-parker</bdi></a></p>
<p dir="auto">For future reference, I’ll post the the full updated script for creating the documents and appending additional text to the top/bottom of each generated document here just in case any future people are interested.</p>
<pre><code># encoding=utf-8
"""in response to https://community.notepad-plus-plus.org/topic/19738/ , the 2022-May-10 add-on

set nChars to be the maximum "chunk" size, replace ENTER YOUR TEXT HERE with your own text, then run this script.
"""
from Npp import editor, notepad
import os.path

nChars = 9000

regex = r'(?s).{1,' + str(nChars) + r'}(\R|\z)'

counter = 0
originalFullname = notepad.getCurrentFilename()
originalPath, originalFile = os.path.split( originalFullname )
originalBase, originalExt = os.path.splitext( originalFile )

def withChunk(m):
    global counter
    counter += 1
    newPath = "{}\\{}_{:03d}{}".format(originalPath, originalBase, counter, originalExt)
    #console.write( "{}\tlength={}".format(newPath, len(m.group(0)))+"\n")

    notepad.new()
    editor.setText(m.group(0))
    editor.documentStart()
    editor.addText('ENTER YOUR TEXT HERE\n\n')
    editor.appendText('ENTER YOUR TEXT HERE')
    notepad.saveAs(newPath)
    notepad.close()
    notepad.activateFile(originalFullname)

editor.research(regex, withChunk)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/76747</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76747</guid><dc:creator><![CDATA[Brent Parker]]></dc:creator><pubDate>Wed, 11 May 2022 01:10:48 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Wed, 11 May 2022 00:16:57 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> amazing, thank you so much!</p>
<p dir="auto">I’m definitely interested in learning the scripting language and what these scripts can do. I’ve been primarily using Notepad++ with RegEx codes to format research articles/papers so that they’re friendly enough to listen to with text-to-speech software.</p>
<p dir="auto">It’s surprisingly time-consuming to edit out the unnecessary bits, but being able to listen to the papers via TTS without it constantly pausing to read every single footnote reference number and citation outloud is definitely helpful for me when I’m just trying to learn/understand the topic of the paper while out on a walk without constant interruptions. This will probably also be helpful when trying to correct/format OCR’d PDF Scanned text of older works that have yet to be digitized.</p>
<p dir="auto">Hopefully, I’ll be able to learn enough to be able to share some tips/tricks with others in the community one day.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76746</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76746</guid><dc:creator><![CDATA[Brent Parker]]></dc:creator><pubDate>Wed, 11 May 2022 00:16:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Tue, 10 May 2022 23:51:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/brent-parker" aria-label="Profile: brent-parker">@<bdi>brent-parker</bdi></a> said in <a href="/post/76744">How to use RegEx to split 5000 characters but preserving sentense?</a>:</p>
<blockquote>
<p dir="auto">Unfortunately, not as simple as I’d hoped</p>
</blockquote>
<p dir="auto">As with most programming languages, you have to put quotes around literal strings.  Fortunately, Python allows using single quotes or double, so using single quotes around the strings that contain double-quotes is easiest:</p>
<pre><code>editor.addText('&lt;speak xmlns="http://www.w3.org/2001/10/synthesis"&gt;&lt;voice name="en-GB"&gt;&lt;prosody rate="-05%"&gt;')
editor.setText(m.group(0))
editor.addText('&lt;/prosody&gt;&lt;/voice&gt;&lt;/speak&gt;')
</code></pre>
<p dir="auto">For someone who has never programmed before, and just tried for an afternoon, good effort.  I’m always happy to see when people in the forum put in the effort to learn, and give it a go, rather than just assuming that we’ll spoon feed them everything.  When you or others show that effort, I feel much better about the effort I put into things like writing the script.  So thank you, and I hope you continue to learn.  For reference, the PythonScript documentation will only teach you about the interface between the plugin and Notepad++ itself;  the default PythonScript plugin uses Python 2.7, and there are a bazillion websites out there which will give tutorials and go into the details of the Python programming language itself.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76745</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76745</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 10 May 2022 23:51:08 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Tue, 10 May 2022 22:55:29 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> hello, sorry to bother you again.</p>
<p dir="auto">I had one last question for the provided script. If I wanted to edit the script so that it appends a predefined set of text before and after the copied chunk to each of files as they are being written, how would I go about doing that?</p>
<p dir="auto">Each file generated would look something like this :</p>
<p dir="auto"><em>&lt;speak xmlns=“<a href="http://www.w3.org/2001/10/synthesis" rel="nofollow ugc">http://www.w3.org/2001/10/synthesis</a>”&gt;&lt;voice name=“en-GB”&gt;&lt;prosody rate=“-05%”&gt;</em></p>
<p dir="auto"><em>TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000</em><br />
<em>TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000  TEXT9000</em></p>
<p dir="auto"><em>&lt;/prosody&gt;&lt;/voice&gt;&lt;/speak&gt;</em></p>
<p dir="auto">I’ve been attempting to read over the <a href="http://npppythonscript.sourceforge.net/docs/latest/index.html" rel="nofollow ugc">PythonScript Documentation</a> this afternoon to try and figure out how to do it, but it’s a bit of a steep learning curve for someone who has never programmed before to be able to learn in one afternoon, lol.  I’d assumed I could just toss in some “editor.addText” items (like below) to place it before and after it sets the copied text:</p>
<pre><code>editor.addText(&lt;speak xmlns="http://www.w3.org/2001/10/synthesis"&gt;&lt;voice name="en-GB"&gt;&lt;prosody rate="-05%"&gt;)
editor.setText(m.group(0))
editor.addText(&lt;/prosody&gt;&lt;/voice&gt;&lt;/speak&gt;)
</code></pre>
<p dir="auto">Unfortunately, not as simple as I’d hoped.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76744</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76744</guid><dc:creator><![CDATA[Brent Parker]]></dc:creator><pubDate>Tue, 10 May 2022 22:55:29 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Tue, 10 May 2022 18:58:37 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> Thank you so much for the amazingly detailed and fast reply!! I just tried it and it worked beautifully! This is going to save me so much time!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76741</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76741</guid><dc:creator><![CDATA[Brent Parker]]></dc:creator><pubDate>Tue, 10 May 2022 18:58:37 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Tue, 10 May 2022 18:53:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/brent-parker" aria-label="Profile: brent-parker">@<bdi>brent-parker</bdi></a> ,</p>
<blockquote>
<p dir="auto"><code>(?s).{1,9000}(\s+|\z)\n</code></p>
</blockquote>
<p dir="auto">Good job adapting it to your situation.  As a gotcha, if your file doesn’t end in a newline character, it won’t grab the very last line in your file.  Since the original post wanted to split anywhere on a space, they used <code>\s+</code>… but since you want to only split on newline, I would suggest <code>(?s).{1,9000}(\R|\z)</code> – where <code>\R</code> matches <code>\r\n</code> or <code>\n</code>, and the <code>\z</code> is the “match end of file” that handles lines that don’t end with a newline.</p>
<blockquote>
<p dir="auto">What would be the simplest way to automatically split/generate 58 separate text files based on the regex code within Notepad++?</p>
</blockquote>
<p dir="auto">Notepad++ does not have a built-in way to automate splitting the file into chunks based on a regex search (or any other method).</p>
<p dir="auto">If you were willing to install the PythonScript plugin, one could write a script which would take the &lt;=9000-char chunks and write them to new files</p>
<pre><code># encoding=utf-8
"""in response to https://community.notepad-plus-plus.org/topic/19738/ , the 2022-May-10 add-on

set nChars to be the maximum "chunk" size, then run this script.
"""
from Npp import editor, notepad
import os.path

nChars = 9000

regex = r'(?s).{1,' + str(nChars) + r'}(\R|\z)'

counter = 0
originalFullname = notepad.getCurrentFilename()
originalPath, originalFile = os.path.split( originalFullname )
originalBase, originalExt = os.path.splitext( originalFile )

def withChunk(m):
    global counter
    counter += 1
    newPath = "{}\\{}_{:03d}{}".format(originalPath, originalBase, counter, originalExt)
    #console.write( "{}\tlength={}".format(newPath, len(m.group(0)))+"\n")

    notepad.new()
    editor.setText(m.group(0))
    notepad.saveAs(newPath)
    notepad.close()
    notepad.activateFile(originalFullname)

editor.research(regex, withChunk)
</code></pre>
<ol start="0">
<li>Install PythonScript plugin (if not already installed) through the Plugins Admin</li>
<li><strong>Plugins &gt; PythonScript &gt; New script</strong>, name = <code>split-file-into-chunks-of-X-characters.py</code></li>
<li>Paste the black box into the script, and save</li>
</ol>
<p dir="auto">To run:</p>
<ol>
<li>Open the file you want to split</li>
<li>click <strong>Plugins &gt; Python Script &gt; scripts &gt; <code>split-file-into-chunks-of-X-characters.py</code></strong></li>
</ol>
<p dir="auto">If you want to assign a keyboard shortcut, use <strong>Plugins &gt; Python Script &gt; Configure…</strong>, and <strong>Add</strong> the script to the left panel.  Exit Notepad++ and restart.  After that, you can use the <strong>Settings &gt;  Shortcut Mapper</strong> to assign the keystroke.  From then on, that keystroke is equivalent to running the script from the Scripts menu.</p>
<p dir="auto">(I tried the script with <code>nChars = 900</code>, and it split the source code for that script into two chunks: one that was 901 characters including the two bytes for the final CRLF (since the regex matches 1 to N characters followed by newline sequence), and one that was 186 characters including final CRLF.)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76740</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76740</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 10 May 2022 18:53:03 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Tue, 10 May 2022 18:07:14 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> Hello, would there be a way to use this RegEx code in Notepad++ to automatically create new  txt files based on the character limits?</p>
<p dir="auto">I edited the RegEx code above a little so that it just selected whole lines up-to a maximum limit of 9000 characters instead of only those ending with a period.</p>
<p dir="auto">(?s).{1,9000}(\s+|\z)\n</p>
<p dir="auto">I have a document that is 499,429 characters long, and when using the code above it gives me a count of 58. What would be the simplest way to automatically split/generate 58 separate text files based on the regex code within Notepad++?</p>
<p dir="auto">Thank you!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76739</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76739</guid><dc:creator><![CDATA[Brent Parker]]></dc:creator><pubDate>Tue, 10 May 2022 18:07:14 GMT</pubDate></item><item><title><![CDATA[Reply to How to use RegEx to split 5000 characters but preserving sentense? on Mon, 20 Jul 2020 11:19:24 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/nz-select" aria-label="Profile: nz-select">@<bdi>nz-select</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">If you don’t mind the <strong>approximation</strong>, as my method considers the <strong>EOL</strong> characters as <strong>standard</strong> ones ( =&gt; <strong><code>\r</code></strong> and/or <strong><code>\n</code></strong> counts for <strong><code>1</code></strong> char ), here are <strong>two</strong> regexes to find <strong>successive</strong> blocks of <strong><code>5000</code></strong> chars or <strong>so</strong>, ending with a <strong>period</strong> :</p>
<ul>
<li>
<p dir="auto">SEARCH <strong><code>(?s).{1,5000}(\.\s+|\z)</code></strong>    will search for the <strong>largest</strong> area, ending with a <strong>period</strong>, with a size <strong>smaller</strong> than <strong><code>5,000</code></strong> characters</p>
</li>
<li>
<p dir="auto">SEARCH <strong><code>(?s).{1,5000}.*?(\.\s+|\z)</code></strong>    will search for the <strong>smallest</strong> area, ending with a <strong>period</strong> with a size <strong>greater</strong> than <strong><code>5,000</code></strong> characters.</p>
</li>
<li>
<p dir="auto">Select the <strong><code>Regular expression</code></strong> search mode and, <strong>tick</strong> the <strong><code>Wrap around</code></strong> option</p>
</li>
</ul>
<p dir="auto"><code> </code></p>
<p dir="auto">To know <strong>how many</strong> blocks of <strong><code>5,000</code></strong> chars or <strong>so</strong>, the <strong>current</strong> file contains, simply hit the <strong><code>Count</code></strong> button, in the <strong>Find</strong> dialog</p>
<hr />
<p dir="auto">Now, in order to find out the <strong>beginning</strong> of the <strong><code>Nth</code></strong> match, use these <strong>generic</strong> regexes :</p>
<ul>
<li>
<p dir="auto">SEARCH <strong><code>(?s)(.{1,5000}(\.\s+|\z)){</code>N-1<code>}\K</code></strong></p>
</li>
<li>
<p dir="auto">SEARCH <strong><code>(?s)(.{1,5000}.*?(\.\s+|\z)){</code>N-1<code>}\K</code></strong></p>
</li>
</ul>
<p dir="auto">And, of course, change the <strong>N - 1</strong> value with the <strong>appropriate</strong> integer !</p>
<p dir="auto"><strong>Remarks</strong> :</p>
<ul>
<li>
<p dir="auto">If a file contains <strong><code>N</code></strong> blocks, in <strong>totality</strong> and you’re using <strong><code>{</code>N<code>}</code></strong> as <strong>quantifier</strong>, it matches the <strong>zero-length</strong> match, at the <strong>very end</strong> of <strong>current</strong> file</p>
</li>
<li>
<p dir="auto">Don’t use a quantifier <strong>superior</strong> to number <strong>N</strong>. And, for <strong>small</strong> files, the <strong>only valid</strong> quantifier <strong><code>{1}</code></strong> will always move to the <strong>very end</strong> of file !</p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
<p dir="auto"><strong>P.S.</strong> :</p>
<p dir="auto">I used the <strong><code>License.txt</code></strong> file to <strong>test</strong> these regexes ! ( <strong><code>4</code></strong> <strong>occurrences</strong> )</p>
]]></description><link>https://community.notepad-plus-plus.org/post/56097</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/56097</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 20 Jul 2020 11:19:24 GMT</pubDate></item></channel></rss>