【发布时间】:2016-09-16 22:27:23
【问题描述】:
我实际上是在尝试在 Atom 中定义一个语法(效果出奇的好),并且在使用 Regex 3 天之后,感觉慢慢地发疯了。
问题是我现在离开了“简单”定义领域,所以我还需要比现在更好的正则表达式知识。
问题:
我想使用begin 和end 匹配4 个特定模式。
通过 Textmate 教程,我了解到行为应该是这样的:
begin: \w,end: \d 变为 \w(.*)\d
利用这些知识,我想匹配这四个表达式:
-
foo( a(1) ):解析为“本身”嵌套的范围(与TextMate Language Example 中的qq-Strings 描述的方式相同。 -
bar(1)('a'):解析为由字段(1)访问的范围bar,因此是字段('a')。bar仅在至少存在第二个括号块的条件下才具有此范围。 -
foo( bar(1)('a') ):(1)和(2)的混合物。foo被提取出来(1),bar代表与(2)中描述的相同的东西。 -
foo( bar(1)('a')('a') )('a'):最复杂的一个。foo表示可以使用第二个括号提取的元素,bar表示可以通过相同机制提取并生成可以访问foo的值,而不会在运行时出现其他问题。
为了捕捉所有这些语句,我现在有两个正则表达式(CSON 语法如下):
'strange_accessors':
{
'comment': 'tries to catch foo(a)(a)(a) constructs'
'begin': '(?:' +
'(?:(?<=\\))\\s*)' + # closing parenthesis beforehand
'|(?:[\\w%\\$\\?!#]*)' + # character beforehand
')' +
'\\s*' +
'(\\()' + # opening bracket
'[^;]+?' +
'(\\))' +
'\\s*(\\()'
'end': '(\\))+?'
'beginCaptures':
'1':
'name': 'punctuation.parens.begin.someLang'
'2':
'name': 'punctuation.parens.someLang'
'3':
'name': 'punctuation.parens.begin.someLang'
'endCaptures':
'0':
'name': 'punctuation.parens.end.someLang'
}
所以,为了抓住括号,我用这个:
'surronding_parenthesis':
{
'comment': 'describes a (nested) accessor using parenthesis'
'begin': '(?:[a-zA-Z_%\\$\\?!#][\\w%\\$\\?!#]*)' + # character beforehand
'(\\()'
'end': '(?>(\\)))'
'beginCaptures':
'1':
'name': 'punctuation.section.parens.begin.someLang'
'endCaptures':
'1':
'name': 'punctuation.section.parens.end.someLang'
'2':
'name': 'banana.invalid.illegal.someLang'
'patterns':[
{ 'include': '#strange_accessors'}
]
}
我在贪婪、不情愿和固执的行为以及原子团中摆弄我的方式,因为我认为这将是成功匹配的关键。
但我很困惑,不知道如何解决这个奇怪的嵌套问题。如果有人有兴趣并想试试我为什么需要这个:
这是Scilab 的语法。
【问题讨论】:
-
递归在哪里?示例 Pcre
(\(((?:[^()]++|(?1))*)\)) -
刚刚尝试过,它匹配所有唯一括号中的模式。但我想要的是更像
foo(foo(b))('a')解析为foo(...)('a')以及foo(b)。
标签: regex coffeescript grammar atom-editor