【问题标题】:Rockstar language parsing Proper VariablesRockstar 语言解析正确的变量
【发布时间】:2020-02-06 02:03:38
【问题描述】:

如何正确正则表达式 Rockstar Language 中的正确变量?

我正在为 vscode 写 syntax highlight extension。但它与字符串文字有点冲突,关键字“says”之后的任何内容都应该是字符串。 但是当与大写单词结合时,它会被检测为适当的变量。

这是我的正则表达式:

(?!(Say|Shout|Whisper|Scream|Listen( to)?|Put|If|Until|Build |Take it to the top|Knock |While|Continue|Break it down|My|Your|A|An|The)\\s)\\b((?<!says .*)([A-Z][a-zA-Z]+\\s?)+)\\b($|\\s)

问题是,非固定长度的lookbehind (?&lt;!says .*) 在vscode 中无法用于解析语法。

Example code snippet on regex101

那么如何做到这一点呢?关于如何不使用该lookbehind的一些想法? 重点是在

Eleanor Rigby says Hello San Francisco

Eleanor Rigby 应该是可变的 says 应该是赋值 Hello San Francisco 应该是字符串

但如果不向后看,它会将Hello San Francisco 视为变量。

【问题讨论】:

    标签: javascript regex visual-studio-code vscode-extensions


    【解决方案1】:

    我认为唯一的方法是消耗您不想要的部分并使用捕获组来识别它(并且显然对其应用特定处理):

    \b(?:(\bsays [A-Za-z\s]+)|(?!(?:Say|Shout|Whisper|Scream|Listen(?: to)?|Put|If|Until|Build|Take it to the top|Knock|While|Continue|Break it down|My|Your|An?|The)\s)\b[A-Z][a-zA-Z]*(?:\s+[A-Z][a-zA-Z]*)*(?!\S))
    

    demo

    【讨论】:

    • 看起来很有希望,我将再添加一个捕获组,将“says”与字符串本身分开\b(?:(\b(says) ([A-Za-z\s]+))|(?!(?:Say|Shout|Whisper|Scream|Listen(?: to)?|Put|If|Until|Build|Take it to the top|Knock|While|Continue|Break it down|My|Your|An?|The)\s)\b([A-Z][a-zA-Z]*(?:\s+[A-Z][a-zA-Z]*)*)(?!\S))
    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多