Notepad++ REGEX 查找并替换倒数第二个斜杠

Notepad++ REGEX find and replace second-last slash

我有一个包含多个路径的大文本文件,如下例所示。路径总是不同的。 我正在 Notepad++ 中寻找正则表达式(查找并替换)以将倒数第二个“/”替换为“/;”。

示例:

/testNAS/questions/ask/test/example/picture.png

替换后:

/testNAS/questions/ask/test/;example/picture.png

我尝试使用正则表达式 /(?=[^/]*$) 但这只标记了最后一个斜杠。

有人能帮帮我吗?

您可以使用

/(?=[^/\v]*/[^/\v]*$)

替换为[=11=];。见 regex demo.

详情

  • / - 斜杠
  • (?=[^/\v]*/[^/\v]*$) - 一个积极的前瞻,需要 / 和垂直空格以外的零个或多个字符,/ 以及 / 和其他零个或多个字符行尾的垂直空格。

[=11=]; 替换模式插入整个匹配值 ([=18=]),然后插入一个 ; 字符来代替匹配。

仅使用您显示的示例,您可以尝试使用正则表达式。

找到什么^(/[^/]*/[^/]*/[^/]*/[^/]*/)(.*)$

替换为;

Online Demo for above regex

说明:为上述正则表达式添加详细说明。

^(                           ##Matching from starting of value in 1st capturing group.
  /[^/]*/[^/]*/[^/]*/[^/]*/  ##Matching / followed by till next occurrence of / doing this 4 times total in here.
)                            ##Closing 1st capturing group here.
(.*)$                        ##Creating 2nd capturing group which has rest of the values here.

您可以使用

^.*/\K[^/\r\n]*/[^/\r\n]*$
  • .*/ 匹配最后一次出现的 /
  • \K 忘记到目前为止匹配的内容
  • [^/\r\n]*/[^/\r\n]* 回溯以使用取反字符匹配 / 的一次出现 class 匹配除前向斜线或换行符以外的任何字符
  • $ 字符串结束

并用分号替换,完整匹配使用 ;[=17=]

Regex demo

试试这个,如果你想替换第五个数字斜线,在你的例子中是从右到左的 second-last..
查找内容:^(.*?\K/){5}
替换为:/;