3

我想在我的产品名称中删除带有数字(参考)或小字(2 个字符或更少)的单词,但我找不到好的正则表达式。

一些例子:

  • “Chaine anti-rebond ECS-2035”应该变成“Chaine anti-rebond”
  • “Guide 35 cm Oregon Intenz”应改为“Guide Oregon Intenz”
  • “Tronçonneuse sans fil AKE 30 LI - Guide 30 cm 36 V”应该变成“Tronçonneuse sans fil AKE - Guide”

我在 PHP 中这样做:

preg_replace('#([^A-Za-z-]+)#', ' ',' '.wd_remove_accents($modele).' ');
4

5 回答 5

4

你不需要在 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 好得多。

于 2012-04-25T12:35:43.660 回答
2

要处理像 in 这样的 unicode 字符,tronçonneuse您可以使用:

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

where\pL代表任何字母,\pN代表任何数字。

于 2012-04-25T12:43:06.057 回答
0

Well, for the combinations in your example the following regex would do:

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

Then just replace the match with an empty string.

However, it doesn't allow for strings like aaa897bbb - just aaa786 or 876aaa (and an optional dash). I don't know what it is that you require - you would have to specify the rules in more detail before the regex can be refined.

于 2012-04-25T12:35:22.660 回答
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);
于 2012-04-25T12:45:39.497 回答
-1

在回调函数http://www.php.net/manual/en/function.preg-replace-callback.php中使用preg_replace_callback和filter

这将适用于所有 3 个测试字符串:

<?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);
于 2012-04-25T12:31:58.707 回答