替换结尾随机并用正则表达式添加字符



我有一个带有随机结束字符串的 url 列表,如下所示:

paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes-8ae0676c-aba2-4cf2-9391-91096a247672
paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4-f707b0fe-31cb-4507-b7b3-7b91695bff9c 
现在我在过去的

几天里一直在尝试找到一个正则表达式来将此行转换为:

/paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes.html
/paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4-f707b0fe-31cb-4507-b7b3-7b91695bff9c.html

问题是随机字符串:

3d0b087-5701-4199-9d9c-147cca687263
33d0b087-5701-4199-9d9c-147cca687263

我需要在没有最后一个的情况下删除这部分 - 并添加.html:在 url 之前添加一个斜杠,如下所示:

我不想要这个:

/paris-chambre-doubletriplequadruple-confort-avec-petit-dejeuner-a-lhotel-de-france-gare-de-lyon-pour-2-a-4-pers-.html

但是这个:

/paris-chambre-doubletriplequadruple-confort-avec-petit-dejeuner-a-lhotel-de-france-gare-de-lyon-pour-2-a-4-pers.html

这是针对运行MySQL 5,PHP 7和Apache 2的新Linux服务器。

您可以在组中捕获要匹配和删除的模式之前的内容。然后在替换中使用第一个捕获组:

^(.*)-[a-f0-9]+(?:-[a-f0-9]+){4,5}$

这将匹配:

  • ^ 字符串开头
  • (.*) 在匹配任何字符 0+ 次的组中捕获
  • -[a-f0-9]+ 匹配连字符,后跟 1+ 乘以 0-9 或 a-f
  • (?:-[a-f0-9]+){4,5} 重复 4-5 次匹配连字符,后跟 1+ 次 0-9 或 a-f
  • $ 字符串结尾

替换为正斜杠和捕获组 1,后跟 .html

/$1.html

正则表达式演示 | PHP 演示

例如

$strings = [
    "paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes-8ae0676c-aba2-4cf2-9391-91096a247672",
    "paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4-f707b0fe-31cb-4507-b7b3-7b91695bff9c"
];
foreach ($strings as $string){
    echo preg_replace('/^(.*)-[a-f0-9]+(?:-[a-f0-9]+){4,5}$/', '/$1.html', $string) . PHP_EOL;
}

结果:

/paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes.html
/paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4.html

所以这些字符串都是相同的格式?

8 - 4 - 4 - 4 - 12 个字母数字字符

那么正则表达式可能是:

/-w{8}-w{4}-w{4}-w{4}-w{12}$/

(w等同于[a-zA-Z0-9]

在PHP中,你会做这样的事情:

$input = "paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes-8ae0676c-aba2-4cf2-9391-91096a247672";
$str = preg_replace("/-w{8}-w{4}-w{4}-w{4}-w{12}$/", "$1.html", $input);

由于您的注释似乎表明唯一的标识子字符串可以在字符串的开头或结尾,那么我建议不要将.html应用于替换 - 只是将其连接/附加到经过净化的字符串中。

将前导/尾随连字符设置为可选,以提高灵活性。

代码:(演示(

$strings = [
    "paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes-8ae0676c-aba2-4cf2-9391-91096a247672",
    "f707b0fe-31cb-4507-b7b3-7b91695bff9c-paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4"
];
foreach ($strings as $string) {
    echo preg_replace(
             '/-?[a-fd]{8}-[a-fd]{4}-[a-fd]{4}-[a-fd]{4}-[a-fd]{12}-?/',
             '',
             $string
         ) . '.html';
    echo "n---n";
}

输出:

paris-chambre-double-classique-avec-option-petit-dejeuner-a-lhotel-trianon-rive-gauche-4-pour-2-personnes.html
---
paris-chambre-double-standard-avec-petit-dejeuner-et-acces-spa-pour-2-personnes-a-lhotel-le-mareuil-4.html
---

最新更新