我有这些标签,我想用另一个标签(链接二)的内容替换一个标签(链接一)的内容。因此,要提取一个标签的内容并将其替换到另一个标签中。例如
<link rel="canonical" href="https://mywebsite.com/link-one.html" />
<a href="/en/link-two.html">
输出应为:
<link rel="canonical" href="https:/mywebsite.com/link-two.html" />
我制作了一个正则表达式,它几乎可以工作,但它必须稍微改进一下。
搜索:
(<link rel="canonical" href=").*?(" \/>)(.*?)(<a href="/en/(.*?)">)
替换为:
\1\5\3\4
(查看.Matches newline
)
答案1
- Ctrl+H
- 找什么:
<link rel="canonical" href="https://mywebsite.com/\K.*?(?=" />.+?<a href="/en/(.*?)">)
- 用。。。来代替:
$1
- 查看 环绕
- 查看 正则表达式
- 查看
. matches newline
- Replace all
解释:
<link rel="canonical" href="https://mywebsite.com/
\K # forget all we have seen until this position
.*? # 0 or more any character, not greedy
(?= # positive lookahead
" /> # literally
.+? # 0 or more any character, not greedy
<a href="/en/ # literally
(.*?) # group 1, 0 or more any character, not greedy
"> # literally
) # end lookahead
替代品:
$1 # content of group 1
截图(之前):
截图(之后):
答案2
对于简单的场景,例如:
<link rel="canonical" href="link-one.html" />
<a href="/en/link-two.html">
(查看.Matches newline
)
寻找:(<link rel="canonical" href=").*?(" \/>)(.*?)(<a href="/en/(.*?)">)
替换为:\1\5\2\3\4
对于第二种情况,例如:
<link rel="canonical" href="https:/mywebsite.com//link-one.html" />
<a href="/en/link-two.html">
(查看.Matches newline
)
寻找:(<link rel="canonical" href=").*?(" \/>)(.*?)(<a href="/en/(.*?)">)
替换为:\1https://mywebsite.com/\5\2\3\4