开发者

My vimgrep search is not behaving as I would expect

开发者 https://www.devze.com 2023-01-15 18:06 出处:网络
I am performing the following vimgrep search (in vim(!)).... :vimgrep /^\\s*bool\\s\\+\\i\\+\\s*=\\s*\\(false\\)\\|\\(true\\);\\s*$/*[files....]*

I am performing the following vimgrep search (in vim(!))....

:vimgrep /^\s*bool\s\+\i\+\s*=\s*\(false\)\|\(true\);\s*$/      *[files....]*

in order to find bool variable initialisations in my code. It successfully returns all of the bool initialisations, e.g.

bool result1 = false;

bool result2=true;

but it also returns other lines where bool are assigned (not initialised), e.g.

开发者_高级运维
result = true;

(i.e. it returns lines even when bool is not found at the start of the line).

I'd be grateful if anybody could tell me why it matches code where there is no "bool" type specifier at the start of the line.

Many thanks,

Steve.


:vimgrep /^\s*bool\s+\i+\s*=\s*(false)\|(true);\s*$/ [files....]
                                 ^     ^^^^   ^

You have some problems, both are marked:

  1. Vim uses \(...\) to group atoms, not (...). Looks like that was the SO parser problem (\( not enclosed with backtics produces ().
  2. You should have \| inside parenthesis: \(false\|true\), or it will take it as «find either a lines where boolean variable is initialized as false (^\s*bool\s+\i+\s*=\s*\(false\) part) or a line which contains true followed by a semicolon at the end of line (\(true\);\s*$ part)».
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号