PHP preg_match words within boundaries
<p>I have some files to scan with patterns using preg_match like:</p>

<p><strong>File name:</strong></p>

<pre><code>(a group: one)
one.txt 

(another group: one-aaa)
one-aaa.txt
one-aaa_1.txt
one-aaa_b.txt
one-aaa_3.txt
one-aaa_whatever.txt

(some other group: one-bbb)
one-bbb.jpg
one-bbb_1.txt
one-bbb_2.txt
one-bbb_t.txt
one-bbb_whatever.txt
</code></pre>

<p>The group is defined by names (hence: one, one-aaa, one-bbb are different groups), and limited to file .txt.</p>

<p>Please do not suggest to use different directories. Those files are already scattered at some directories, I need a way to find matches by keyword, not directories.</p>

<p>Now I can define groups manually by specifying "one", "one-aaa", etc, but have trouble with preg_match. My preg_match returns "one" and "one-aaa" as a single group:</p>

<pre><code>$keyword = 'one';
$match = '/(^)' . $keyword . '(.*\.txt$)/';

$match = '/\b(' . $keyword . ')\b(.*\.txt$)/';
</code></pre>

<p><strong>The expected return:</strong>
one.txt</p>

<p><strong>Unexpected returns:</strong>
one.txt
one-aaa.txt, etc</p>

<p><strong>UPDATE 1:</strong>
When keyword changed to 'one-aaa', I want it return: one-aaa.txt, one-aaa_1.txt, and the likes.
The way I group is:</p>

<pre><code>$keyword = str_replace('_', ' ', $file-&gt;name);
returns: one, one-aaa, one-bbb, etc
</code></pre>

<p>What I want to say in plain English: </p>

<ol>
<li>find matches that start with "one", returns: one_1.txt, one_2.txt</li>
<li>find matches that start with "one-aaa", returns: one-aaa_1.txt, one-aaa_2.txt, etc</li>
</ol>

<p>Can anyone shed the light on the correct regex?</p>

<p>Thanks</p>

<p><strong>UPDATE 2:</strong>
Somebody here previously provided suggestion to avoid greedy regex, and use .*? instead, but the answer was deleted. It finally works this way as per his suggestion:</p>

<pre><code>$match = '/^\b(' . $keyword . ')\b(.*?.txt$)/';
</code></pre>

<p>Who should I assign an answer to now? Can anyone volunteer to write a working answer like above, or a betterment of it?</p>

<p><strong>UPDATE 3:</strong>
Oops, I talked too soon. It didn't work, but the key was reset somewhow when I change key|value pairs thats why I lost track of the double inclusion. Sorry the above still no go.</p>

<p><strong>UPDATE 4:</strong>
I finally made it with additional condition to simply exclude the output, if they don't match the group. Extra codes and also extra scanning, bad, but at least it works as expected by now. Still using the suggested regex above. 
Still looking for ultimate regex solution, if any. If no, then "no" should be the chosen answer</p>

<p>Thanks</p>
