Regex for deleting words with numbers

I would like to delete words with numbers (link) or small words (2 characters or less) in my product name, but I cannot find a good regular expression.

Some examples:

  • "Chaine anti-rebond ECS-2035" should become "Chaine anti-rebond"
  • "Guide 35 see Oregon Intenz" should become "Guide Oregon Intenz"
  • "Tronçonneuse sans fil AKE 30 LI - Manual 30 cm 36 V" should be "Tronçonneuse sans fil AKE - Manual"

I do this in PHP:

preg_replace('#([^A-Za-z-]+)#', ' ',' '.wd_remove_accents($modele).' ');
+3
source share
5 answers

You do not need to do everything you know in RegExp:

<?php

$str = "Chaine anti-rebond ECS-2035 cm 30 v";
$result = array();

$split = explode(" ", $str); //Split to an array

foreach ($split as $word) {
    if ((strlen($word) <= 2) || (preg_match("|\d|", $word))) {  //If word is <= 2 char long, or contains a digit
        continue;                                               //Continue to next iteration immediately 
    }
    $result[] = $word;                                          //Add word to result array (would only happen if the above condition was false)
}

$result = implode(" ", $result);                                //Implode result back to string

echo $result;

, , , , RegExp.

+4

Unicode, tronçonneuse, :

/\b(?:[\pL-]+\pN+|\pN+[\pL-]+|\pN+|\pL{1,2})\b/

\pL , \pN .

+2

, regex:

/\b(?:[-A-Za-z]+[0-9]+|[0-9]+[-A-Za-z]+|\d{1,2}|[A-Za-z]{1,2})\b/

.

aaa897bbb - aaa786 876aaa ( ). , - , regex .

0

, :

$subject = 'Tronçonneuse sans fil AKE 30 LI - Guide 30 cm 36 V';
$regex = '/(\\s+\\w{1,2}(?=\\W+))|(\\s+[a-zA-Z0-9_-]+\\d+)/';
$result = preg_replace($regex, '', $subject);
0

Use preg_replace_callback and filter in the callback function http://www.php.net/manual/en/function.preg-replace-callback.php

This will work for all three test lines:

<?php

$str = "Tronçonneuse sans fil AKE 30 LI - Guide 30 cm 36 V";

function filter_cb($matches)
{
    $word = trim($matches[0]);

    if ($word !== '-' && (strlen($word) <= 2 || (preg_match("/\d/", $word)))) {
        return '';
    }

    return $matches[0];
}

$result = preg_replace_callback('/([\p{L}\p{N}-]+\s*)/u', "filter_cb", $str);

echo trim($result);
-1
source

All Articles