当捕获组太复杂时,为什么“捕获”在 tmLanguage 中不起作用?
我想知道为什么捕获组变得太复杂时无法确定捕获组。
我创建了以下正则表达式,它仅匹配第一组。
(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))))+
- 等号
(=)
编辑
我注意到我的正则表达式有点混乱,所以这是一个更好的方法,但也不起作用:
/(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))))+
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论