【问题标题】:Character escaping in keymap file键盘映射文件中的字符转义
【发布时间】:2019-05-29 12:37:02
【问题描述】:

这是自动配对星号(用于 Markdown 和 AsciiDoc)文件的键盘映射。有用。问题:

1) 我应该或不应该在第 1 块的第 6 行转义星号吗?

2) 在我的测试中,这一行中的转义没有任何区别。但为什么?如果您尝试在最后一个块的第 5 行和第 6 行(以及第 5 个块中的行)中删除转义,则会导致不正确的行为。所以,这里需要转义 。但似乎第一个街区不需要它。这让我很困惑。

[
    // Auto-pair *
    { "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
            { "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\\*a-zA-Z0-9_]$", "match_all": true }, // --- THIS line ---
            { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true }
        ]
    },
    { "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
        ]
    },
    { "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true }, // --- THIS line ---
            { "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
            { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
        ]
    },
    { "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": "\\*$", "match_all": true }, // --- THIS line ---
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true }, // --- THIS line ---
            { "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
            { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
        ]
    },
]

更新:实际上,使用另一个正则表达式会更好:

- [*a-zA-Z0-9_]$
+ (^[*\\s]*$)|([*a-zA-Z0-9_]$)

这样,当您使用项目符号列表时,星号不会在行首重复。

【问题讨论】:

    标签: regex escaping sublimetext3 sublimetext


    【解决方案1】:

    在正则表达式中,* 运算符很特殊,它表示前一个原子的零次或多次重复。因此,为了匹配文字 * 字符,您需要将其转义为 \*(或 JSON 中的 \\*),以向正则表达式引擎表明您的意思是文字 * 而不是应用特殊含义。

    构造[] 表示character set,它匹配集合中的任何字符。在字符集中,对正则表达式引擎具有特殊含义的唯一字符是](关闭集合)、\(仍然需要能够转义)、-(指定字符范围)和^(否定集合,仅当它是第一个字符时才特殊),因此只有那些字符需要在集合内转义。

    由于字符集意味着匹配以下任何一个字符* 的特殊含义不适用,因此在字符集中它不需要转义(尽管您如果您愿意,仍然可以这样做)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-08
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多