正则表达式替换未括在方括号中的单词 [英] Regex replace word when not enclosed in brackets

查看:60
本文介绍了正则表达式替换未括在方括号中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个正则表达式,以替换括号中没有的单词.

I'm trying to create a regular expression where it replaces words which are not enclosed by brackets.

这是我目前拥有的:

$this->parsed = preg_replace('/\b(?<!\[)('.preg_quote($word).')\b/','[$1['.implode(",",array_unique($types)).']]',$this->parsed);

其中$ word可能是以下其中之一,"Burkely Mayfair Trunk"或"Trunk".

Where $word could be one of the following, "Burkely Mayfair Trunk" or "Trunk".

它将替换句子

这个Burkely Mayfair Trunk非常漂亮

This Burkely Mayfair Trunk is pretty nice

这个[Burkely Mayfair [Trunk [产品名称]] [产品名称]]很漂亮 很好

This [Burkely Mayfair [Trunk[productname]][productname]] is pretty nice

尽管它应该变成

这个[Burkely Mayfair Trunk [产品名称]]很好

This [Burkely Mayfair Trunk[productname]] is pretty nice

由于按最大字符串替换为最小字符串的顺序,较小的字符串和/或重复出现的单词部分不应替换为已替换的字符串部分.当它是字符串的第一部分时,它可以工作.

Since it replaces in order of the largest string to the smallest string, the smaller strings and or double occurences of word parts should not be replaced in an already replaced part of the string. It works when it's the first part of the string.

当我尝试在后面进行动态查找时,会出现以下错误:编译失败:lookbackhind声明在偏移11处的长度不是固定的".而且我不知道如何解决这个问题.

When I try to make a dynamic lookbehind it gives the following error: "Compilation failed: lookbehind assertion is not fixed length at offset 11". And I have no idea on how to fix this.

有什么想法吗?

推荐答案

您可以使用\[[^][]*]模式匹配括号内的任何子字符串,然后使用

You may match any substring inside parentheses with \[[^][]*] pattern, and then use (*SKIP)(*FAIL) PCRE verbs to drop the match, and only match your pattern in any other context:

\[[^][]*](*SKIP)(*FAIL)|your_pattern_here

请参见 regex演示.要跳过成对的嵌套方括号内的匹配,请使用带有子例程的基于重新安装的正则表达式(注意,它必须使用捕获组):

See the regex demo. To skip matches inside paired nested square brackets, use a recusrsion-based regex with a subroutine (note it will have to use a capturing group):

(?<skip>\[(?:[^][]++|(?&skip))*])(*SKIP)(*FAIL)|your_pattern_here

参见 regex演示

此外,由于要动态构建模式,因此需要preg_quote $word以及定界符(此处为/).

Also, since you are building the pattern dynamically, you need to preg_quote the $word along with the delimiter symbol (here, /).

您的解决方案是

$this->parsed = preg_replace(
    '/\[[^][]*\[[^][]*]](*SKIP)(*FAIL)|\b(?:' . preg_quote($word, '/') . ')\b/', 
    '[$0[' . implode(",", array_unique($types)) . ']]',
    $this->parsed);

\[[^][]*\[[^][]*]]正则表达式将匹配用替换模式包装的所有匹配项:

The \[[^][]*\[[^][]*]] regex will match all those occurrences that have been wrapped with your replacement pattern:

  • \[-一个[
  • [^][]*-除[]
  • 之外的0+个字符
  • \[-一个[ char
  • [^][]*-除[]
  • 之外的0+个字符
  • ]]-一个]]子字符串.
  • \[ - a [
  • [^][]* - 0+ chars other than [ and ]
  • \[ - a [ char
  • [^][]* - 0+ chars other than [ and ]
  • ]] - a ]] substring.

这篇关于正则表达式替换未括在方括号中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆