regex - 在 gedit 中,突出显示括号内的函数调用

标签 regex recursion syntax-highlighting gedit

我当前正在编辑 javascript.lang 文件以突出显示函数名称。 这是我当前使用的 gtksourceview 的表达式。

<define-regex id="function-regex" >
(?&lt;=([\.|\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 where b is what begins the construct, m is what can occur in the middle of the construct, and e is what can occur at the end of the construct. For correct results, no two of b, m, and e 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/

相关文章:

java - 是什么导致我的 Java `.matches()` 正则表达式返回 False?

java - 为什么我的正则表达式在 shell 脚本中失败,尽管它在 jregex 中工作?

objective-c - 递归地从 JSON 结构中删除空值

javascript - 确定递归函数何时完成

用于匹配所有不是指定字符串的字符串的javascript正则表达式

java - 正则表达式中的可选前瞻

c++ - 当初始条件为假时进入循环

vim + tmux 视觉模式不突出显示

Thor 的 VIM 中类似 Ruby 的语法高亮显示

syntax-highlighting - 如何在 Jekyll 中高亮 Markdown 代码?