正则表达式捕获带有 {{ }} 符号的奇怪字符串
Regex catching a weird string with {{ }} signs
字符串示例
{{ai|Perseverance|Garen}} is no longer removed by combat dehancing effects such as {{ai|Blood Scent|Warwick}}.
我想从这个 {{ai| |Garen}}
中捕捉 {{ai|Perseverance|Garen}}
并保持中间部分与 {{ai| |Warwick}}
相同
这是我想出的:
(\{\{ai\|)\w+(\|\w+\}\})
它非常接近第一个,但是有了 Perserverance,我可以做两次正则表达式来摆脱第一个 {{ai| and then |Garen}}
首先使用 (\{\{ai\|)
然后 (\|\w+\}\})
.
有没有办法在一个正则表达式中做到这一点?
更新:我使用 PHP 只是为了清楚
您的匹配项在以下正则表达式的第 1 组中
\{\{[^|]*\|([^|]*)
我假设您想删除 {{some|
和 |thing}}
部分并保留中间部分。
您可以使用
{{[^|]*\|([^|]*)\|[^|]*}}
和</code>作为替换模式。</p>
<p>正则表达式表示:</p>
<ul>
<li><code>{{
- 两个文字 {
符号
[^|]*\|
- |
以外的零个或多个字符(带有 [^|]*
)后跟文字 |
([^|]*)
- 第 1 组匹配 |
以外的零个或多个字符
\|
- 文字 |
[^|]*
- |
以外的零个或多个字符
}}
- 收盘 }}
见demo
请注意,当 {{
/}}
.
中有 3 个 |
分隔的部分时,它将起作用
在PHP中,您可以使用following code:
$re = '~{{[^|]*\|([^|]*)\|[^|]*}}~';
$str = "{{ai|Perseverance|Garen}} is no longer removed by combat dehancing effects such as {{ai|Blood Scent|Warwick}}";
$result = preg_replace($re, '', $str);
echo $result;
// => Perseverance is no longer removed by combat dehancing effects such as Blood Scent
字符串示例
{{ai|Perseverance|Garen}} is no longer removed by combat dehancing effects such as {{ai|Blood Scent|Warwick}}.
我想从这个 {{ai| |Garen}}
中捕捉 {{ai|Perseverance|Garen}}
并保持中间部分与 {{ai| |Warwick}}
相同
这是我想出的:
(\{\{ai\|)\w+(\|\w+\}\})
它非常接近第一个,但是有了 Perserverance,我可以做两次正则表达式来摆脱第一个 {{ai| and then |Garen}}
首先使用 (\{\{ai\|)
然后 (\|\w+\}\})
.
有没有办法在一个正则表达式中做到这一点?
更新:我使用 PHP 只是为了清楚
您的匹配项在以下正则表达式的第 1 组中
\{\{[^|]*\|([^|]*)
我假设您想删除 {{some|
和 |thing}}
部分并保留中间部分。
您可以使用
{{[^|]*\|([^|]*)\|[^|]*}}
和</code>作为替换模式。</p>
<p>正则表达式表示:</p>
<ul>
<li><code>{{
- 两个文字 {
符号
[^|]*\|
- |
以外的零个或多个字符(带有 [^|]*
)后跟文字 |
([^|]*)
- 第 1 组匹配 |
\|
- 文字 |
[^|]*
- |
}}
- 收盘 }}
见demo
请注意,当 {{
/}}
.
|
分隔的部分时,它将起作用
在PHP中,您可以使用following code:
$re = '~{{[^|]*\|([^|]*)\|[^|]*}}~';
$str = "{{ai|Perseverance|Garen}} is no longer removed by combat dehancing effects such as {{ai|Blood Scent|Warwick}}";
$result = preg_replace($re, '', $str);
echo $result;
// => Perseverance is no longer removed by combat dehancing effects such as Blood Scent