I’m trying to match the contents that belong between a certain ( and its matching ) as found by vim when using the motion %.
More specifically, I’m looking for a regex that looks like this hypothetical /someKeyword (\{pair}\(.*\))\{pair}/, if there were such modifiers as \{pair} that when applied to two exactly two characters in a regex, makes the second one only match if it’s the matching bracket to the first one (%-wise).
The pattern I’m looking for should match the inner contents of the first bracket following someKeyword (n.b. the code that it should work on is always correctly bracketed), as in the examples:
For someKeyword ("aaa") the submatch will match "aaa".
Likewise someKeyword ("aaa)") will match "aaa)" and someKeyword(("double-nested stuff")) will match ("double-nested stuff")
But also in cases like:
(
someKeyword("xyz"))
where it should match "xyz".
Is there any way to make use of vim’s matching bracket functionality in regexes? And if not, what other solution might work to accomplish this?
Edit 1: the matched contents may span several lines.
This is not possible with vim regular expressions (as language that allows such nested constructs is not regular), but is possible with ‘regular’ expressions provided by perl (as well as by other languages I do not know enough to be sure) and perl can be used from inside vim. I don’t like vim-perl bindings (because it is very limited), but if you know all cases that should work, then you could use recursion feature of perl regular expressions (requires newer perl, I have 5.12*):
Note that if can avoid such regular expressions, you should do it (because you depend on re compiler too much), so I suggest to use vim motions directly:
You can use something like
instead of operatorfunc to save and restore registers, but the above code leaves all registers and marks untouched, what I can’t guarantee with saved* stuff. It also guarantees that if you remove
join()aroundtext, you will save information about the location of NULLs (if you care about them, of course). It is not possible with registers variant.