当捕获组太复杂时,为什么“捕获”在 tmLanguage 中不起作用?

发布于 2025-01-15 20:32:22 字数 1068 浏览 2 评论 0原文

我想知道为什么捕获组变得太复杂时无法确定捕获组。

我创建了以下正则表达式,它仅匹配第一组。

(type)(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+(\&)?(\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?((((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+)?(=)

当我将其更改为以下正则表达式时,捕获可以应用于捕获中的适当范围:

(type)(\s+)([a-zA-Z0-9]+)(\s+)(=)

发生这种情况是因为它是简化的正则表达式,还是因为我的捕获组出了问题?

正则表达式的细分

1:查找类型关键字

(type)

2:空格或注释

(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+

3:变量名称

(\&)?(\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?

4:空格或注释

(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+
  1. 等号
(=)

编辑

我注意到我的正则表达式有点混乱,所以这是一个更好的方法,但也不起作用:

/(type)((\/\*([^\*\/]+)?\*\/)|(\/\/([^\n]+)?\n)| |\t)+((\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?)((\/\*([^\*\/]+)?\*\/)|(\/\/([^\n]+)?\n)| |\t)+(=)/

I'm wondering why captures isn't able to determine the capture groups when they get too complex.

I have created the following regex, and it only matches the first group.

(type)(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+(\&)?(\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?((((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+)?(=)

When I change it to the following regex, the captures can be applied to the appropriate scopes in captures:

(type)(\s+)([a-zA-Z0-9]+)(\s+)(=)

Is this happening because it is a simplified regex, or because something is wrong with my capture groups?

Breakdown of regex

1: Find the type keyword

(type)

2: Whitespace or a comment

(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+

3: Variable name

(\&)?(\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?

4: Whitespace or a comment

(((( |\t))|((\/\*(([^\*\/])+)?\*\/))|((\/\/(([^\n])+)?\n))))+
  1. Equal sign
(=)

EDIT

I noticed my regex is a bit messy, so here's a better one that also doesn't work:

/(type)((\/\*([^\*\/]+)?\*\/)|(\/\/([^\n]+)?\n)| |\t)+((\$|\_|[a-zA-Z])((\$|\_|[a-zA-Z0-9])+)?)((\/\*([^\*\/]+)?\*\/)|(\/\/([^\n]+)?\n)| |\t)+(=)/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文