admin管理员组文章数量:1415420
I'm looking at writing a C++ syntax file, in which I hope to be able to highlight some blocks and constructs.
One thing I want to highlight is an if
block. I've figured out how to highlight the whole block, by using a region with start and end markers. But now what I'd like to do is highlight parts of the block in different colours.
Specifically, I'd like to highlight the text within the parentheses, even if that text contains "()" itself.
For example, in the following block:
if (this is the() clause) {
do something;
do another thing;
}
I'd like to highlight the text "this is the() clause"
, but I cannot figure out the regex for this.
I have tried another region, which does capture some text within the parentheses:
:syntax region cppClause start=/\v\(/hs=e+1 end=/\v\)/he=s-1
But a) it stops the match if there is a )
character in the text,
and b) I'm uncertain if I can nest this region, within the bigger if
region which I have captured.
Is there a simpler way to do this?
I'm looking at writing a C++ syntax file, in which I hope to be able to highlight some blocks and constructs.
One thing I want to highlight is an if
block. I've figured out how to highlight the whole block, by using a region with start and end markers. But now what I'd like to do is highlight parts of the block in different colours.
Specifically, I'd like to highlight the text within the parentheses, even if that text contains "()" itself.
For example, in the following block:
if (this is the() clause) {
do something;
do another thing;
}
I'd like to highlight the text "this is the() clause"
, but I cannot figure out the regex for this.
I have tried another region, which does capture some text within the parentheses:
:syntax region cppClause start=/\v\(/hs=e+1 end=/\v\)/he=s-1
But a) it stops the match if there is a )
character in the text,
and b) I'm uncertain if I can nest this region, within the bigger if
region which I have captured.
Is there a simpler way to do this?
Share Improve this question asked Feb 11 at 12:38 centauricentauri 754 bronze badges 1 |2 Answers
Reset to default 2For the regex patterns below I am using PRCE2 regex flavor, that should work with vim
If you want to capture the contents of the outer parenthesis on any one line, you can use this regex pattern (PRCE2 regex flavor):
/\s\((.*)\)\s/g
Regex Demo: https://regex101/r/s7V8zk/9
NOTES:
\s
Start matching one whitespace before the first opening parenthesis.\(
Match literal(
, the first opening parenthesis.(.*)
The capture group for the contents to be highlighted with different color. The dot (.
) special character will match all characters except newline, including parenthesis. The dot followed by the star quantifier.*
will match zero or more characters, and as many characters as possible to make a match, i.e. the*
quantifier alone is greedy. The dot.
matches all characters except newline character\n
, so the.*
will capture all characters until the very last)
before the end of line to make a match.\)
Match literal)
, the last closing parenthesis.\s
Match one whitespace character.
If you are only looking to match the contents of the outer parenthesis on an if-line, this regex pattern will do that job (PRCE2 regex flavor):
(?:^|\s)if\s(?:\w*\s)*\((.*)\).*\{
Regex Demo: https://regex101/r/K82Pml/6
NOTES:
(?:...)
Non-capturing group(?:^|\s)
Match beginning of text^
or|
whitespace character\s
, including newlineif
Matches a literalif
\s
Match one whitespace(?:\w*\s)*
Match 0 or more*
word characters\w
(letters, numbers, underscore) followed by a whitespace character\s
. Match this pattern 0 or more times(...)*
.\(
Match literal(
, the first opening parenthesis.(.*)
The capture group for the contents to be highlighted with different color. The dot (.
) special character will match all characters except newline, including parenthesis. The dot followed by the star quantifier.*
will match zero or more characters, and as many characters as possible to make a match, i.e. the*
quantifier alone is greedy. The dot.
matches all characters except newline character\n
, so the.*
will capture all characters until the very last)
before the end of line to make a match.\)
Match literal)
, the last closing parenthesis..*
Match 0 or more characters, as many characters as possible (greedy)\}
Match literal{
at the end of line. To make sure you have a proper if line withif
followed by something inside parenthesis followed by}
at the end.
You could use the vi(
and it'll visualize whatever inside the next parentheses after your cursor, if that's what you mean.
本文标签:
版权声明:本文标题:regex - How can I capture the text inside a pair of parentheses, if that text may itself contain parentheses, in Vim? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745211430a2647908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
:help :syn-keepend
which has an example like yours. You can define nested syntax groups so you'd have to define something that takes care of the inner parens. Getting it right can be tricky. – Friedrich Commented Feb 11 at 14:02