tags:

views:

37

answers:

1

Hi,

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.

+7  A: 
: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)».
ZyX
Thanks for the answer - the second part has nailed it - I was forgetting about the high precedence of the \| operator.NB. \(\(false\)\|\(true\)\) works as well. Putting the false and true statements in parenthesis seems more obvious to me as then \(true\) and \(false\) are both atoms and I want either one of them to be matched.But if \(false\|true\) also works (which it does - I've checked it!) then does that mean that false and true on their own are also atoms? If so then how would I write an expression which matched:falserue and falstrue
sdrjones
I've found my own answer - /fals\(e\|t\)rue/ would match falserue and falstrue! I think my confusion was another result of misunderstanding the precedence of \| in vim patterns.
sdrjones