【问题标题】:Capture groups not working in Sublime Text RegReplace package捕获组在 Sublime Text RegReplace 包中不起作用
【发布时间】:2017-01-11 03:15:06
【问题描述】:

这是原始的 HTML:

<td class="danish">newtext</td>
<td><?php audioButton("../../audio/lessons/01/oldtext","oldtext"); ?></td>
<td><?php audioButton("../../audio/lessons/01/slow/oldtext_slx","oldtext_slx","1"); ?></td>

我想使用 Sublime Text 2 中的 RegReplace 包将 'oldtext' 替换为 'newtext'。

这是我在reg_replace.sublime-settings中的(不用分析整个东西,重点是里面有几个(.*)):

{
"replacements": {
    "audiobutton_rep": {
                "find": "<td class=\"danish\">(.*)</td>\\n.*<td><\\?php audioButton\\(\"(.*)/.*\",\".*\"\\); \\?></td>\\n.*<td><\\?php audioButton\\(\"(.*)/.*\",\".*\",\"1\"\\);.*\\?></td>",
                "replace": "<td class=\"danish\">$1</td>\n<td><?php audioButton(\"$2/$1\",\"$1\");?></td>\n<td><?php audioButton(\"$3/$1_slx\",\"$1_slx\",\"1\");?></td>",
                "greedy": true,
                "case": false
    },

这是default.sublime-commands中的命令:

{
    "caption": "Reg Replace: XXXXXXXXXXXXXXXX",
    "command": "reg_replace",
    "args": {"replacements": ["audiobutton_rep"] }
 },

捕获组不起作用,因此输出如下所示:

<td class="danish">$1</td>
<td><?php audioButton("$2/$1","$1");?></td>
<td><?php audioButton("$3/$1_slx","$1_slx","1");?></td>

当我自己运行它时它可以完美运行,但不是在这里。

【问题讨论】:

  • 在这种情况下可能不是很有帮助,但请记住:stackoverflow.com/a/1732454/2224996
  • 好吧 :) 但它在大多数情况下都有效。也许我的运气终于用完了。

标签: regex sublimetext2


【解决方案1】:

您应该使用\\1\\2\\3 而不是$1$2$3

Sublime Text 的内置搜索/替换使用 Boost 库,它接受替换字符串中的 $1\1,而大多数其他风格只接受 $1。但是 RegReplace 是用 Python 编写的,它只使用反斜杠。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多