<?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[Regex: Find words between words]]></title><description><![CDATA[<p dir="auto">I have <code>a gift for</code> my mother. I want <code>a nice book for</code> myself. I love <code>a beautiful woman for</code> her eyes.</p>
<p dir="auto">So, as you can see, I want to find all words expression starting with <code>a</code> and ending with <code>for</code>. But for not more then 6 words between them.</p>
<p dir="auto">I made a regex, but it is not so good.</p>
<p dir="auto"><code>\bA[\s\S]*?\w+{1,6}\bFOR[\s\S]*?</code></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/16269/regex-find-words-between-words</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 03:15:07 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/16269.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 04 Sep 2018 16:43:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex: Find words between words on Wed, 05 Sep 2018 03:39:53 GMT]]></title><description><![CDATA[<p dir="auto">thank you</p>
<p dir="auto">also,</p>
<p dir="auto"><code>a(\W+\w+){1,6}\W+for</code></p>
]]></description><link>https://community.notepad-plus-plus.org/post/34604</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/34604</guid><dc:creator><![CDATA[Vasile Caraus]]></dc:creator><pubDate>Wed, 05 Sep 2018 03:39:53 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Find words between words on Tue, 04 Sep 2018 23:59:50 GMT]]></title><description><![CDATA[<p dir="auto">Hello <strong><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3278">@vasile-caraus</a></strong>, <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3841">@peterjones</a> and <strong>All</strong>,</p>
<p dir="auto">Why not this regex :</p>
<p dir="auto"><strong><code>a\s+(\w+\s+){1,6}?for</code></strong></p>
<p dir="auto">OR</p>
<p dir="auto"><strong><code>a\h+(\w+\h+){1,6}?for</code></strong></p>
<p dir="auto">With the <strong>sample</strong> text, below, it matches, only, in sentences <strong><code>2</code></strong> to <strong><code>7</code></strong> !</p>
<pre><code class="language-diff">1 : If was a for sale ( Incorrect sentence ! )
2 : It was a house for sale.
3 : It was a small house for sale.
4 : It was a small old house for sale.
5 : It was a very small old house for sale.
6 : It was a very small old green house for sale.
7 : It was a very small old green wooden house for sale.
8 : It was a very small old green wooden house designed for sale.
9 : It was a very small old green wooden house not designed for sale.
</code></pre>
<p dir="auto">With a <strong>single</strong> long line of text, joining lines <strong><code>1</code></strong> to <strong><code>9</code></strong>, it works nice too !</p>
<pre><code class="language-diff">If was a for sale ( Incorrect sentence ! ) It was a house for sale. It was a small house for sale. It was a small old house for sale. It wasavery small old house for sale. It was a very small old green house for sale. It was a very small old green wooden house for sale. It was a very small old green wooden house designed for sale. It was a very small old green wooden house not designed for sale.
</code></pre>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/34595</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/34595</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Tue, 04 Sep 2018 23:59:50 GMT</pubDate></item><item><title><![CDATA[Reply to Regex: Find words between words on Tue, 04 Sep 2018 19:11:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3278">@Vasile-Caraus</a> ,</p>
<p dir="auto">First, may I say, thank you for showing example data and what you had previously tried.  That make it so much easier to help you.</p>
<p dir="auto">I’ve got a solution that I think will work: <code>(?i)\bA\b(?:\s*\b\w+\b\s*){1,6}\bFOR\b</code>  See it described at <a href="https://regexr.com/3uuht" rel="nofollow ugc">https://regexr.com/3uuht</a>.</p>
<p dir="auto">In your sequence, the problems I see:</p>
<ul>
<li><code>[\s\S]*?</code> means a non-greedy selection of 0 or more spaces or non-spaces – that means it non-greedy matches anything.  I am not sure that’s what you really intended</li>
<li><code>\w+{1,6}</code>: one or more word characters (from <code>\w+</code>), followed by nothing repeated 1 to 6 times.  This is actually an error in the regex, and probably will cause the regex to do nothing (NPP Find says “invalid regular expression”). If you want the <code>{1,6}</code> to apply to the groupings of one-or-more word characters, you have to parenthesize around the <code>\w+</code>, as shown</li>
</ul>
<p dir="auto">I fixed those in my version.</p>
<p dir="auto">I also added a couple more word boundaries, just to be explicit, and to prevent some false matches that I think may or may not be what you want.</p>
<p dir="auto">Caveats in my  interpretation:</p>
<ul>
<li><code>(?i)</code>: I was explicit about case insensitive (otherwise <code>A</code> and <code>FOR</code> would not match <code>a</code> and <code>for</code>)</li>
<li>I wanted to make it match <code>a day for</code>, but not <code>an evening for</code>, because you said “a”.  If you want it to be able to start with <code>a</code> or <code>an</code>, but not <code>and</code>, use <code>\bAN*\b</code></li>
<li>Since you said “for”, I assumed you wanted to match <code>a day for</code>, but not <code>a day to go forth</code> nor <code>a day to go forward</code>.  If you want the latter as well, then <code>\bFOR\w*\b</code></li>
<li>You weren’t explict as to whether you wanted the space after “for” to be included in the match or not (your final [\s\S] kindof hinted you do, but I wasn’t sure).  If you do, then <code>\bFOR\b\s+</code></li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/34585</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/34585</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 04 Sep 2018 19:11:40 GMT</pubDate></item></channel></rss>