正则表达式在VS Code中落后吗? [英] Regex look behind in VS Code?

查看:152
本文介绍了正则表达式在VS Code中落后吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS Code中进行语法扩展,但是在查看正则表达式模式时遇到了困难.给定以下字符串,我只想在 @fmt(

I'm working on a grammar extension in VS Code, and I'm having difficulty with a look behind regex pattern. Given the following string, I want to only return cmp when it's preceded by the @fmt(

@fmt(cmp,foo)

我在另一个编辑器中使用的匹配字符串是:

The matching string I used in another editor was this:

(?<=[@|©](fmt)\()(\w+)

但是,这在VS Code中不起作用,当我进行正则表达式搜索时,它会返回错误,提示它不是有效的表达式.玩弄它时,问题在于< = 字符,它表示后面的样子.

However, this is not working in VS Code, when I do a regex search it comes back with the error that it's not a valid expression. Playing around with it, the problem is the <= characters, which indicate the look behind.

搜索VS Code网站不会返回任何正则表达式参考指南.搜索堆栈溢出出现在

Doing a search of the VS Code website doesn't return any kind of regex reference guide. Searching Stack Overflow came up with this question, which states that Visual Studio has a unique regex definitions. Unfortunately, the example given in that question doesn't work in VS Code.

有人知道如何在VS Code中查看正则表达式吗?或者至少知道VS Code的正则表达式文档在哪里?

Does anyone know how to do a look behind regex in VS Code? Or at least know where the regex documentation for VS Code is located?

我担心这是不可能的,因为根据堆栈溢出参考 JavaScript不支持回头看.有另一个问题,它显示了如何模仿外观JavaScript函数中的落后功能,但我不知道是否可以使用用户定义的函数来扩展VS Code中的语言.如果有人知道该怎么做,并且可以指出我的方向,那也是可以接受的解决方法.

I worry that it's not possible, since according to Stack Overflow reference look behinds aren't supported in JavaScript. There is another question that shows how to mimic look behinds in a JavaScript function, but I don't know if it's possible to extend a language in VS Code with user-defined functions. If anyone knows how to do that, and can point me in that direction, that would also be an acceptable workaround.

推荐答案

向前看,看看这两个在VS Code中的工作.下载一些现有的语言插件,并查看其规则,以了解如何制定它们.这是我的ANTLR语法扩展的一部分:

Look ahead and look behind both work in VS Code. Download some of the existing language plugins and look at their rules, to learn how this must be formulated. Here's a part from my ANTLR grammar extension:

{
  "name": "meta.rule.parser.antlr",
  "begin": "[[:lower:]][[:alnum:]_]*(?=.*?:)",
  "beginCaptures": {
    "0": { "name": "entity.name.function.parser.antlr" }
  },
  "end": "(?<=;)",
  "patterns": [
    {
      "name": "variable.other",
      "match": "\\[.*?\\]"
    },
    {
      "name": "keyword.other.antlr",
      "match": "\\b(returns|locals)\\b"
    },
    {
      "name": "keyword.other.antlr",
      "match": "@\\w+"
    },
    {
      "name": "entity.other.rule.option",
      "match":" <.*?>"
    },
    {
      "name": "variable.other.antlr",
      "match": "\\w+\\s*="
    },
    {
      "name": "entity.name.tag.antlr",
      "match": "#.*"
    },

    { "include": "#comments" },
    { "include": "#strings" },
    { "include": "#lexer-rule-reference" },
    { "include": "#parser-rule-reference" },
    { "include": "#predicate" },
    { "include": "#action" },
    { "include": "#wildcard-regexp" },
    { "include": "#regexp-parts" },
    { "include": "#options-list" }

  ]
}

末尾比赛使用后视图案.另外请记住,您可以嵌套模式,即可以为更大的结构(例如整个功能)定义开始/结束,然后将其部分与子模式进行匹配(并分配单独的样式).这就是为什么您通常不需要向前/向后看的原因,因为您知道自己在特定的代码块中.

The end match uses a look behind pattern. Keep also in mind you can nest patterns, i.e. you can define begin/end for a bigger structure (e.g. an entire function) and then match parts of it with sub patterns (and assign the individual style). This why you often don't need look ahead/behind because you know you are in a certain block of code.

对于上面的图案:(\w+)部分是否应该是后向图案的一部分?如果是这样,应该在最后一个右括号之前.

For your pattern above: is the (\w+) part supposed to be part of the lookbehind pattern? If so it should be before the last closing parenthesis.

这篇关于正则表达式在VS Code中落后吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆