Vim Tips Wiki
(Added categories)
(Added to Searching category + minor reformatting)
Line 9: Line 9:
 
|rating=0/2
 
|rating=0/2
 
|text=
 
|text=
Q: How to do a search that will find both of the following examples?
+
'''Q:''' How to do a search that will find both of the following examples?
 
 
for (
 
for (
  +
int i=0;
 
int i=0;
+
i<3;
 
i++
 
i<3;
 
 
i++
 
 
 
)
 
)
   
 
&
 
&
 
 
for ( int i=0; i<3; i++)
 
for ( int i=0; i<3; i++)
   
 
--------------------------------------
 
--------------------------------------
   
A: Use this pattern
+
'''A:''' Use this pattern
 
\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})
 
\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})
 
 
 
   
 
Explanation:
 
Explanation:
 
* <tt>\&lt;for\&gt;</tt> //Match the word "for"
   
 
*<tt>[ ^I\n]\{-0,}</tt> //Match any whitespace (space " ", tab "^I", newline "\n") 0 or more times with a non-greedy search (the negative makes it non-greedy)
\&lt;for\&gt; //Match the word "for"
 
   
[ ^I\n]\{-0,} //Match any whitespace (space " ", tab "^I", newline "\n") 0 or more times with a non-greedy search (the negative makes it non-greedy)
 
   
   
 
Why go to all this trouble instead of searching for <tt>\&lt;for\&gt;</tt>?
 
Why go to all this trouble instead of searching for \&lt;for\&gt;?
 
   
 
Lets say I wanted to find all places where I do a for loop against MAX_INT, I could say:
 
Lets say I wanted to find all places where I do a for loop against MAX_INT, I could say:
 
/\&lt;for\&gt;[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*MAX_INT.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})
 
/\&lt;for\&gt;[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*MAX_INT.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})
 
 
   
   
Line 54: Line 42:
 
== Comments ==
 
== Comments ==
 
another possibility using \_, which adds newline matching to various patterns:
 
another possibility using \_, which adds newline matching to various patterns:
 
\&lt;for\&gt;\_s*(\_s*.\{-};\_s*.\{-};\_s.\{-}\_s*)
 
\&lt;for\&gt;\_s*(\_s*.\{-};\_s*.\{-};\_s.\{-}\_s*)
 
   
 
may or may not satisfy your requirement exaclty, but works for the given examples.
 
may or may not satisfy your requirement exaclty, but works for the given examples.
Line 62: Line 49:
 
, September 6, 2006 15:24
 
, September 6, 2006 15:24
 
----
 
----
\&lt;for\&gt;\_s*(\(\_s*.*;\)\{2}\_s.*\_s*)
+
\&lt;for\&gt;\_s*(\(\_s*.*;\)\{2}\_s.*\_s*)
   
 
may suffice as well for the first 2 examples.
 
may suffice as well for the first 2 examples.
Line 72: Line 59:
 
[[Category:C]]
 
[[Category:C]]
 
[[Category:C plus plus]]
 
[[Category:C plus plus]]
  +
[[Category:Searching]]

Revision as of 12:17, 24 July 2007

Previous TipNext Tip

Tip: #1320 - Search for a C-style for statement

Created: September 6, 2006 9:24 Complexity: intermediate Author: MrQBerrt<at>yahoo.com Version: 5.7 Karma: 0/2 Imported from: Tip#1320

Q: How to do a search that will find both of the following examples?

for ( 
  int i=0; 
  i<3; 
  i++ 
) 

&

for ( int i=0; i<3; i++) 

A: Use this pattern

\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,}) 

Explanation:

  • \<for\> //Match the word "for"
  • [ ^I\n]\{-0,} //Match any whitespace (space " ", tab "^I", newline "\n") 0 or more times with a non-greedy search (the negative makes it non-greedy)


Why go to all this trouble instead of searching for \<for\>?

Lets say I wanted to find all places where I do a for loop against MAX_INT, I could say:

/\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*MAX_INT.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,}) 


Please add your own patterns to simplify this, or to match other common code bits. Also, if anyone knows how to match this with :grep, let me know.

Comments

another possibility using \_, which adds newline matching to various patterns:

\<for\>\_s*(\_s*.\{-};\_s*.\{-};\_s.\{-}\_s*) 

may or may not satisfy your requirement exaclty, but works for the given examples.

Anonymous , September 6, 2006 15:24


\<for\>\_s*(\(\_s*.*;\)\{2}\_s.*\_s*) 

may suffice as well for the first 2 examples.

Anonymous , September 6, 2006 15:32