【问题标题】:Regular Expression Transformation Interpretation正则表达式转换解释
【发布时间】:2012-12-30 16:26:12
【问题描述】:

我在Sublime Text 2 工作,为goto:@ command 定义符号。我需要帮助解释这个正则表达式转换:

<key>symbolTransformation</key>
<string>s/\/\*\*\s*(.*?)\s*\*\//** $1 **/; s/\/\*.*?\*\*\//./; s/\/\*[^\*].*?[^\*]\*\///</string>

我的意图是编辑转换,但我需要先了解它。

有没有关于学习这种特殊语法的参考资料?我对正则表达式比较陌生,所有的 \ 和 / 让我发疯! :)

如果做不到这一点,有人可以帮我介绍一下吗?

【问题讨论】:

    标签: regex text expression sublimetext2 transformation


    【解决方案1】:

    如果我们“解包”正则表达式,它们分别是:

    • 将/\*\*\s*(.*?)\s*\*/替换为** $1 **;
    • 将/\*.*?\*\*/替换为.;
    • 将/\*[^*].*?[^*]\*/ 替换为空字符串(空字符串)。

    第一个正则表达式:

    /\*\*   # Look for '/' followed by two occurrences of '*',
    \s*     # followed by zero or more space characters,
    (.*?)   # followed by zero or more of any character, lazily (with capture),
    \s*     # followed by zero or more space characters,
    \*/     # followed by a '*' and a '/'
    

    由于捕获中的量词是惰性的 (*?),这意味着正则表达式引擎将在满足正则表达式的下一个组件 (\s*) 之前尝试匹配尽可能少的文本(与正常情况不同,“贪婪”量词,如*,如果需要,它将在返回文本以满足下一个组件之前尝试匹配最大可能的数量)。捕获在替换文本中以$1 的形式提供。

    第二个正则表达式:

    /\*      # Look for a '/' then a '*',
    .*?      # followed by zero or more of any character, lazily,
    \*\*/    # followed by two '*' and a '/'
    

    并将所有这些替换为一个点 (.)。

    第三个正则表达式:

    /\*      # Look for a '/' then a '*',
    [^*]     # followed by one character which is not a '*',
    .*?      # followed by zero or more of any character, lazily,
    [^*]     # followed by one character which is not a '*',
    \*/      # followed by a '*' then a '/'.
    

    并用空字符串替换所有这些。

    【讨论】:

    • 很好的解释!谢谢。
    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多