我当前正在编辑 javascript.lang 文件以突出显示函数名称。 这是我当前使用的 gtksourceview 的表达式。
<define-regex id="function-regex" >
(?<=([\.|\s]))
([a-z]\w*)
(?=([\(].*))(?=(.*[\)]))
</define-regex>
这是正则表达式本身
(?<=([\.|\s]))([a-z]\w*)(?=([\(].*))(?=(.*[\)]))
它似乎适用于我满意的 foo(A) 等情况。 但我遇到麻烦的是,如果我希望它突出显示另一个函数调用的括号内的函数名称。
foo(bar(A))
或者更严格地说
foo{N}(foo{N-1}(...(foo{2}(foo{1}(A))...))
举个例子,
foo(bar(baz(A)))
我的目标是突出显示 foo、bar、baz,仅此而已。
我不知道如何处理 bar 功能。我读过有关使用 (?R) 或 (?0) 递归执行正则表达式的方法,但我没有成功地使用它在 gedit 中递归突出显示函数。
附注 以下是我目前用来确定成功的测试。
initialDrawGraph(toBeSorted);
$(element).removeClass(currentclass);
myFrame.popStack();
context.outputCurrentSortOrder(V);
myFrame.nextFunction = sorter.Sort.;
context.outputToDivConsole(formatStr(V),1);
最佳答案
平衡括号不是正则表达式,因为它需要内存(参见:Can regular expressions be used to match nested patterns?)。对于某些实现,有一个实现 recursion in regular expressions :
Matching Balanced Constructs
The main purpose of recursion is to match balanced constructs or nested constructs. The generic regex is
b(?:m|(?R))*e
whereb
is what begins the construct,m
is what can occur in the middle of the construct, ande
is what can occur at the end of the construct. For correct results, no two ofb
,m
, ande
should be able to match the same text. You can use an atomic group instead of the non-capturing group for improved performance:b(?>m|(?R))*e
.A common real-world use is to match a balanced set of parentheses.
\((?>[^()]|(?R))*\)
matches a single pair of parentheses with any text in between, including an unlimited number of parentheses, as long as they are all properly paired. If the subject string contains unbalanced parentheses, then the first regex match is the leftmost pair of balanced parentheses, which may occur after unbalanced opening parentheses. If you want a regex that does not find any matches in a string that contains unbalanced parentheses, then you need to use a subroutine call instead of recursion. If you want to find a sequence of multiple pairs of balanced parentheses as a single match, then you also need a subroutine call.
关于regex - 在 gedit 中,突出显示括号内的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740895/