preg_replace 确定年份并将 wiki 代码添加到 link

preg_replace to identify a year and add wiki codes to link it

问。任何人都可以帮助正则表达式正确识别每边有空格的 4 位数字吗?

我有一个 wiki 用于记录历史、时间表。这个想法是识别年份,每边有空格的 4 位数字,然后每边添加 wiki 代码 [[ & ]],这样年份就会自动变成 link 到它的页面同名。 (不包括当年的页面,不需要 link 本身)。我有一个半工作答案,但注意到它自动 linked 一个介于 ., 之间的数字。

我目前的 php 是; ($this->GetPageTag() = wiki 页面名称)

// Autolink years - 
if (ctype_digit($this->GetPageTag()) && strlen($this->GetPageTag()) == '4') 
{
  // This is a year page and should not have the numbers auto linked.
} else {
  // This is anything BUT a year page so we can auto link any years. 
  $body = preg_replace('!(\b\d{4}\b)!', '[[]]', $body);
  // Fix problem of double bracketing, bit of a hack for the above really.
  $body = str_replace("[[[[","[[",$body);
  $body = str_replace("]]]]","]]",$body);
}
// end autolink years.

所以有问题的代码是; $body = preg_replace('!(\b\d{4}\b)!', '[[$1]]', $body);

谢谢

在环视中用 space 替换单词边界:

 $body = preg_replace('!(?<= )(\d{4})(?= )!', '[[]]', $body);

如果你还想匹配表格:

 $body = preg_replace('!(?<=\h)(\d{4})(?=\h)!', '[[]]', $body);

I have a semi working answer but noticed it auto linked a number between a . and ,.

Word boundaries \b 在以下位置匹配:

Before the first character in the string, if the first character is a word character.

After the last character in the string, if the last character is a word character.

Between two characters in the string, where one is a word character and the other is not a word character.

这意味着,\b\d{4}\b 匹配 ,1990. 字符串中的 1990

要确保匹配空格之间的字符串或字符串的 start/end,请使用

$body = preg_replace('~(?<!\S)\d{4}(?!\S)~', '[[[=10=]]]', $body);

参见regex demo

详情

  • (?<!\S) - 不允许紧邻当前位置左侧的非空白字符
  • \d{4} - 4 位数字
  • (?!\S) - 不允许紧靠当前位置右侧的非空白字符。

在替换字符串中,[=20=]替换反向引用用于插入整个匹配值(无需捕获所有正则表达式模式)。

而且在这种情况下不需要删除4个[],除非你在原文中有它们。