【问题标题】:Regex to replace character between square brackets, but not between parenthesis正则表达式替换方括号之间的字符,但不替换括号之间的字符
【发布时间】:2020-02-28 10:02:03
【问题描述】:

我有一个如下所示的 Markdown 链接列表:

[filename-with-some-words](/001-folder/filename-with-some-words.md)
[title-with-other-words](/001-folder/title-with-other-words.md)
[other-words](/001-folder/other-words.md)

我想替换出现在方括号[] 之间的-,而不是出现在括号() 之间的那些,这样我的最终结果将如下所示:

[filename with some words](/001-folder/filename-with-some-words.md)
[title with other words](/001-folder/title-with-other-words.md)
[other words](/001-folder/other-words.md)

我可以将括号中的所有文本与(?<=\[).+?(?=\])匹配,但我如何只匹配破折号-

【问题讨论】:

    标签: regex sublimetext3 regex-lookarounds


    【解决方案1】:

    你可以使用

    (?:\G(?!\A)|\[)[^][-]*\K-(?=[^][]*])
    

    用空格替换。请参阅regex demo

    详情

    • (?:\G(?!\A)|\[) - 上一次成功匹配的结束或[
    • [^][-]* - 除][- 之外的任何 0+ 个字符
    • \K - 匹配重置运算符从整个匹配内存缓冲区中丢弃到目前为止匹配的所有文本
    • - - 一个连字符
    • (?=[^][]*]) - 后面是 0+ 个非括号字符,直到 ] 字符。

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-10-26
      • 2018-09-26
      • 2016-09-20
      • 1970-01-01
      相关资源
      最近更新 更多