替换 url 中的字符串和字符
Replacing strings and characters inside urls
我正在尝试替换 url 中以特定模式开头的字符,并在末尾添加“.html”。我正在使用 Sublime Text,但无法完全使用它。
我想做 3 个开关:
- https://my.master.com 到 https__my.master.com
- 在每个 url
的末尾添加“.html”
- 将 url 中的“/”替换为“_”
重要提示:仅适用于以 href="https://my.master.com/@
开头的 urls
示例:
href="https://my.master.com/@top-com/d/my-zen/"
我想得到的结果:
href="https__my.master.com_@top-com_d_my-zen_.html"
到目前为止我尝试过的:
在 Sublime Text 中,我尝试将其放入查找字段中:
href="https://my.master.com/@([^\"]+)
在替换字段内:
href="https__my.master.com_@.html"
它适用于 1) 和 2) 但不适用于 3)。我不知道如何将“/”替换为“_”
我用正则表达式得到的输出:
https__my.master.com_@top-com/d/my-zen/.html
这不是用于生产的东西
如果你更容易帮助我,我不介意连续做 2 次查找和替换。
提前致谢!
根据您可能拥有的子目录数量,您可能会使用类似于以下的一些表达式:
href="https:\/\/my\.master\.com\/@([^\/]*)\/([^\/]*)\/([^\/]*)\/"
href="https:\/\/my\.master\.com\/@([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/"
并用一些类似的字符串替换它们:
href="https:__my.master.com_@___.html"
href="https:__my.master.com_@____.html"
几次。
Demo
如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何匹配一些样本输入。
如果 Notepad++ 适合您,您可以执行以下操作,不幸的是,它不适用于 SublimeText。
这适用于 URL 中任意数量的斜杠。
- Ctrl+H
- 查找内容:
(?:https|\G).*?\K:?/(")?
- 替换为:
_(?1.html:)
- 选中环绕
- 检查正则表达式
- 取消勾选
. matches newline
- 全部替换
解释:
(?: # non capture group
https # literally
| # OR
\G # restart from last match position
) # end group
.*? # 0 or more any character but newline, not greedy
\K # forget all we have seen until this position
:? # optional colon
/ # slash
(")? # group 1, a double quote, optional
替换:
_ # underscore
(?1 # if group 1 exists (i.e. there is a double quote)
.html # literally
# content of group 1
: # else
# nothing
) # endif
给定:
href="https://my.master.com/@top-com/d/my-zen/"
href="https://my.master.com/my-zen/"
href="https://my.master.com/@top-com/d/e/f/g/my-zen/"
给定示例的结果:
href="https__my.master.com_@top-com_d_my-zen_.html"
href="https__my.master.com_my-zen_.html"
href="https__my.master.com_@top-com_d_e_f_g_my-zen_.html"
屏幕截图(之前):
屏幕截图(之后):
我正在尝试替换 url 中以特定模式开头的字符,并在末尾添加“.html”。我正在使用 Sublime Text,但无法完全使用它。
我想做 3 个开关:
- https://my.master.com 到 https__my.master.com
- 在每个 url 的末尾添加“.html”
- 将 url 中的“/”替换为“_”
重要提示:仅适用于以 href="https://my.master.com/@
开头的 urls示例:
href="https://my.master.com/@top-com/d/my-zen/"
我想得到的结果:
href="https__my.master.com_@top-com_d_my-zen_.html"
到目前为止我尝试过的:
在 Sublime Text 中,我尝试将其放入查找字段中:
href="https://my.master.com/@([^\"]+)
在替换字段内:
href="https__my.master.com_@.html"
它适用于 1) 和 2) 但不适用于 3)。我不知道如何将“/”替换为“_”
我用正则表达式得到的输出:
https__my.master.com_@top-com/d/my-zen/.html
这不是用于生产的东西
如果你更容易帮助我,我不介意连续做 2 次查找和替换。
提前致谢!
根据您可能拥有的子目录数量,您可能会使用类似于以下的一些表达式:
href="https:\/\/my\.master\.com\/@([^\/]*)\/([^\/]*)\/([^\/]*)\/"
href="https:\/\/my\.master\.com\/@([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/"
并用一些类似的字符串替换它们:
href="https:__my.master.com_@___.html"
href="https:__my.master.com_@____.html"
几次。
Demo
如果您希望 simplify/modify/explore 表达式,regex101.com. If you'd like, you can also watch in this link 的右上面板已对其进行说明,它将如何匹配一些样本输入。
如果 Notepad++ 适合您,您可以执行以下操作,不幸的是,它不适用于 SublimeText。
这适用于 URL 中任意数量的斜杠。
- Ctrl+H
- 查找内容:
(?:https|\G).*?\K:?/(")?
- 替换为:
_(?1.html:)
- 选中环绕
- 检查正则表达式
- 取消勾选
. matches newline
- 全部替换
解释:
(?: # non capture group
https # literally
| # OR
\G # restart from last match position
) # end group
.*? # 0 or more any character but newline, not greedy
\K # forget all we have seen until this position
:? # optional colon
/ # slash
(")? # group 1, a double quote, optional
替换:
_ # underscore
(?1 # if group 1 exists (i.e. there is a double quote)
.html # literally
# content of group 1
: # else
# nothing
) # endif
给定:
href="https://my.master.com/@top-com/d/my-zen/"
href="https://my.master.com/my-zen/"
href="https://my.master.com/@top-com/d/e/f/g/my-zen/"
给定示例的结果:
href="https__my.master.com_@top-com_d_my-zen_.html"
href="https__my.master.com_my-zen_.html"
href="https__my.master.com_@top-com_d_e_f_g_my-zen_.html"
屏幕截图(之前):
屏幕截图(之后):