Mediawiki链接的正则

发布于 2025-02-07 19:00:50 字数 1146 浏览 1 评论 0原文

我需要帮助尝试修复以在MediaWiki网站上使用的正则GEGEX,尤其是在链接领域。

我要完成的是转动[[< name> (<标识符>)|< name>]]{{{< template> |< |< link> | |> |>}}}}}} 我似乎是我制作的。搞砸了它的工作方式。

以这两个字符串为例:

====[[<link #1>]] [[<link #2 name> (<link #2 identifier>)|<link #2 name>]]====
[[<link #3 name> (<link #3 identifier>)|<link #3 name>]]

我的正则是/\ [\ [(。*?)\((。*??)\)\ |(。*??)] 在情况下,结果应该刚刚捕获[[&lt; link#2 name&gt; (&lt; link#2标识符&gt;)|&lt; link#2名称&gt;]][[&lt; link#3 name&gt; (&lt; link#3标识符&gt;)|&lt; link#3 name&gt;]]

相反,Regex捕获[[&lt; link#1&gt;]] (&lt; link#2标识符&gt;)|&lt; link#2名称&gt;]][[&lt; link#3 name&gt; (&lt; link#3标识符&gt;)|&lt; link#3名称&gt;]],当我替换为{{&lt; template&gt; | $ 1 | $ 2}}} AS:

===={{<template>|<link #1>]] [[<link #2 name>|<link #2 identifier>}}====
{{<template>|<link #3 name>|<link #3 identifier>}}

我需要帮助。我尝试在互联网上查找,但我找不到一种方法来防止正则试图与其他链接重叠

I need help trying to fix a regex to use on a MediaWiki site, particularly in the field of links.

What I want to accomplish is turning [[<name> (<identifier>)|<name>]] to just {{<template>|<link>|<identifier>}} but the regex I made seems to be screwing around with how it works.

Take these two strings for instance:

====[[<link #1>]] [[<link #2 name> (<link #2 identifier>)|<link #2 name>]]====
[[<link #3 name> (<link #3 identifier>)|<link #3 name>]]

My regex is /\[\[(.*?) \((.*?)\)\|(.*?)]]/gm
Under circumstances, the result should have just caught [[<link #2 name> (<link #2 identifier>)|<link #2 name>]] and [[<link #3 name> (<link #3 identifier>)|<link #3 name>]]

Instead, the regex captures [[<link #1>]] [[<link #2 name> (<link #2 identifier>)|<link #2 name>]] and [[<link #3 name> (<link #3 identifier>)|<link #3 name>]], leaving the end result when I replace with {{<template>|$1|$2}} as:

===={{<template>|<link #1>]] [[<link #2 name>|<link #2 identifier>}}====
{{<template>|<link #3 name>|<link #3 identifier>}}

I need help with this. I tried looking around the internet, and I can't find a way to prevent the regex from trying to overlap with other links

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-02-14 19:00:50

您可以使用

\[\[((?:(?!\[\[)[^()])*)\s+\(((?:(?!\[\[)[^()])*)\)\|(.*?)]]

regex demo 详细信息

  • \ [\ [ - a [[ string
  • (((?:(?)) ))*) - 第1组:除以外的零或更多字符( and 每个都不启动[[[ substring
  • \ s+ - 一个或多个whitespaces
  • \( - a )( char
  • ((? [)[^()])*) - 第2组:零或更多字符以外的其他字符( and 每个都不启动[[ substring
  • \)\ | - a )| String
  • (。*?) - 组3:任何零或除线断裂字符以外的其他字符
  • ] - ]]字符串。

You can use

\[\[((?:(?!\[\[)[^()])*)\s+\(((?:(?!\[\[)[^()])*)\)\|(.*?)]]

See the regex demo. Details:

  • \[\[ - a [[ string
  • ((?:(?!\[\[)[^()])*) - Group 1: zero or more chars other than ( and ) each of which does not start a [[ substring
  • \s+ - one or more whitespaces
  • \( - a ( char
  • ((?:(?!\[\[)[^()])*) - Group 2: zero or more chars other than ( and ) each of which does not start a [[ substring
  • \)\| - a )| string
  • (.*?) - Group 3: any zero or more chars other than line break chars as few as possible
  • ]] - a ]] string.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文