【问题标题】:Notepad++ regexp ID of the sub-expressions in the double parentheses双括号中子表达式的 Notepad++ 正则表达式 ID
【发布时间】:2021-03-12 17:01:36
【问题描述】:

我试图在 Notepad++ 字符串中找到这样的:

'<a href="/mp3files/cards/Sentence With any Number of Words.mp3"></a>',

并将它们转换成这样:

'<a href="/mp3files/cards/sentence-with-any-number-of-words.mp3"></a>',

我制作了一个正则表达式来裁剪以cards/ 开头并以&lt;/a&gt; 结尾的字符串:

(cards/)([^\s]{1,50})(([\s\.\?\!\-\,])(\w{1,50}))+(\.mp3"></a>)

或者另一种方法:

(cards/)([^\s]{1,50})([\s\.\?\!\-\,]{0,})([^\s]{1,50})

两者都适用于搜索,但我无法获得替代品。 问题是一个句子中的单词数量可能会有所不同。 而且我无法获取双括号中的子表达式的ID。

以下替换格式:\1\2\3... 不起作用,因为我无法获得双括号中子表达式的正确 ID。 我试图用谷歌搜索该主题,但找不到任何东西。非常感谢任何建议、链接或最好的完整替换表达式。

【问题讨论】:

  • 哇,这么简单,非常感谢!

标签: replace expression notepad++


【解决方案1】:

这会将/cards/ 之后的所有空格替换为连字符并小写文件名。


  • Ctrl+H
  • 查找内容:(?:href="/mp3files/cards/|\G)\K(?!\.mp3)(\S+)(?:\h+|(\.mp3))
  • 替换为:\L$1(?2$2:-)
  • 检查 环绕
  • CHECK 正则表达式
  • 全部替换

说明:

(?:                     # non capture group
    href="/mp3files/cards/  # literally
  |                       # OR
    \G                      # restart fro last match position
)                       # end group
(?!\.mp3)               # negative lookahead, make sure we haven't ".mp3" after this position
\K                      # forget all we have seen until this position
(\S+)                   # group 1, 1 or more non spaces
(?:                     # non capture group
    \h+                     # 1 or more horizontal spaces
  |                       # OR
    (\.mp3)                 # group 2, literally ".mp3"
)                       # end group

替换:

\L$1            # lowercase content of group 1
(?2             # if group 2 exists (the extension .mp3)
    $2              # use it
  :               # else
    -               # put a hyphen
)               # endif

屏幕截图(之前):

截图(之后):

【讨论】:

  • 再次感谢托托(赞成)。您是否愿意在替换中将更改添加为小写?
  • @Ken:有点复杂,看我的编辑。
  • 托托,非常感谢。这对我来说真的很有帮助和教育意义。
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多