(Replace category 'C plus plus' with 'C++') |
(Change <tt> to <code>, perhaps also minor tweak.) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
|previous=1319 |
|previous=1319 |
||
|next=1321 |
|next=1321 |
||
− | |created= |
+ | |created=2006 |
|complexity=intermediate |
|complexity=intermediate |
||
|author=QBerrt |
|author=QBerrt |
||
|version=5.7 |
|version=5.7 |
||
|rating=0/2 |
|rating=0/2 |
||
+ | |category1=C |
||
+ | |category2=C++ |
||
⚫ | |||
}} |
}} |
||
'''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? |
||
Line 14: | Line 17: | ||
for ( |
for ( |
||
int i=0; |
int i=0; |
||
− | i |
+ | i<3; |
i++ |
i++ |
||
) |
) |
||
Line 21: | Line 24: | ||
and |
and |
||
<pre> |
<pre> |
||
− | for ( int i=0; i |
+ | for ( int i=0; i<3; i++) |
</pre> |
</pre> |
||
'''A:''' Use this pattern |
'''A:''' Use this pattern |
||
<pre> |
<pre> |
||
− | \ |
+ | \<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,}) |
</pre> |
</pre> |
||
Explanation: |
Explanation: |
||
− | *< |
+ | *<code>\<for\></code> //Match the word "for" |
− | *< |
+ | *<code>[ ^I\n]\{-0,}</code> //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 < |
+ | Why go to all this trouble instead of searching for <code>\<for\></code>? |
Let's say I wanted to find all places where I do a for loop against MAX_INT. I could say: |
Let's say I wanted to find all places where I do a for loop against MAX_INT. I could say: |
||
<pre> |
<pre> |
||
− | /\ |
+ | /\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*MAX_INT.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,}) |
</pre> |
</pre> |
||
Line 43: | Line 46: | ||
Another possibility using \_, which adds newline matching to various patterns: |
Another possibility using \_, which adds newline matching to various patterns: |
||
<pre> |
<pre> |
||
− | \ |
+ | \<for\>\_s*(\_s*.\{-};\_s*.\{-};\_s.\{-}\_s*) |
</pre> |
</pre> |
||
Line 51: | Line 54: | ||
This may suffice as well for the first 2 examples: |
This may suffice as well for the first 2 examples: |
||
<pre> |
<pre> |
||
− | \ |
+ | \<for\>\_s*(\(\_s*.*;\)\{2}\_s.*\_s*) |
</pre> |
</pre> |
||
---- |
---- |
||
− | [[Category:C]] |
||
− | [[Category:C++]] |
||
⚫ |
Latest revision as of 06:20, 13 July 2012
Tip 1320 Printable Monobook Previous Next
created 2006 · complexity intermediate · author QBerrt · version 5.7
Q: How to do a search that will find both of the following examples?
for ( int i=0; i<3; i++ )
and
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\>
?
Let's 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,})
Comments[]
Another possibility using \_, which adds newline matching to various patterns:
\<for\>\_s*(\_s*.\{-};\_s*.\{-};\_s.\{-}\_s*)
May or may not satisfy your requirement exactly, but works for the given examples.
This may suffice as well for the first 2 examples:
\<for\>\_s*(\(\_s*.*;\)\{2}\_s.*\_s*)