I'm beginner for normal expression. so facing following problem.

my code is the following.

$originalStr="101 abc 1101 xyz";
preg_match_all('/[0-9]{3,5} /', $originalStr, $matches);
$result=array();
$result=array_unique($matches[0]);
sort($result);
global $wpdb;
for($i=0;$i<count($result);$i++)
{   

    $getName="SELECT Name FROM MyTable WHERE ProductID='".trim($result[$i])."' LIMIT 1";
    $data = $wpdb->get_results($getName);
    $replaceWith="";
    foreach ($data as $obj)
    {
        $replaceWith=$obj->Name;
        $originalStr=str_replace($result[$i], $result[$i]." : ".$replaceWith, $originalStr);        
        break;      
    }   
}
echo $originalStr;

here my purpose would be to replace product id by 'product id : product Name' but however , as 1101 is getting 101 since it's sub number so it's changing it 1101's string with also 101's string.

so i get variety of numbres fine but my issue is with alternative. I would like it to be changed with exact numbermatch only.

maybe improve your regexp to something similar to this preg_replace_callback

You could utilize [cde] rather than by hand looping within the result list and changing it inside a next step (with str_replace not being conscious of context or which to exchange).

preg_replace_callback

So simply do your database query within the callback. It isn't super efficient to complete multiple queries inside a loop, nor for the reason that callback. But it is simpler a minimum of. And $newStr = preg_replace_callback('/([0-9]{3,5}) /', "cb_repl", $originalStr); function cb_repl($match) { list ($all, $num) = $match; db("SELECT Name FROM MyTable WHERE ProductID='$num' LIMIT 1"); ... return "{$num} : {$replaceWith} "; // just the replacement string } knows which part it matched up and replaces this exact substring using the came back alternative.