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

查看:16
本文介绍了正则表达式在 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 网站不会返回任何类型的正则表达式参考指南.搜索 Stack Overflow 想出了 这个问题,其中指出 Visual Studio 有一个独特的正则表达式定义.不幸的是,该问题中给出的示例在 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天全站免登陆