Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think <strong>@Kent</strong>'s answer (the function) is the "most correct" when doing complex searches/substitutions. It's easier to change afterward, and to understand it also after a while, it's also quicker to write than this. But I think it can be interesting if it's possible to do this with a "oneliner".<br> I welcome comments about this not working, I find it interesting myself if it's possible, <strong>not</strong> if it's the "best solution".</p> <p>A "oneliner" for all situations described in the original question.</p> <pre><code>:v/str3/s/\v(.*str1&amp;.*str2)@=.{-}\zsxxx/yyy/g </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><code>:v/str3/</code> all lines NOT containing "<code>str3</code>"</li> <li><code>s/</code> substitute</li> <li><code>\v</code> "very magic" (to not have to escape regex expressions like <code>{}</code>), <a href="http://vimdoc.sourceforge.net/htmldoc/pattern.html#/%5Cv" rel="nofollow"><code>:h \v</code></a></li> <li><code>(.*str1&amp;.*str2)@=</code> positive look-ahead, <a href="http://vimdoc.sourceforge.net/htmldoc/pattern.html#/%5C@=" rel="nofollow"><code>:h \@=</code></a> <ul> <li><code>.*str1</code> any character 0 or more ("greedy") followed by "<code>str1</code>", the preceding <code>.*</code> is important for both <code>&amp;</code> and <code>@=</code> to work</li> <li><code>&amp;</code> pattern before and after <code>&amp;</code> must be present, different than <code>|</code> that is using "or", both patterns <strong>must</strong> start at same place, thats why <code>.*</code> is used before <code>str1/2</code>, <a href="http://vimdoc.sourceforge.net/htmldoc/pattern.html#/%5C&amp;" rel="nofollow"><code>:h \&amp;</code></a></li> <li><code>@=</code> the atom before (in the parentheses) should be present, but the pattern is not included in the match, the atom must however start at the beginning of the line for this to work (<code>.*</code> makes sure of that).</li> </ul></li> <li><code>.{-}</code> any character 0 or more ("not greedy"), <code>{-}</code> instead of <code>*</code> is important if more than one occurrence of <code>xxx</code> should be substituted (not enough with the <code>g</code> flag in this case)</li> <li><code>\zs</code> sets the start of the match (what to substitute).</li> <li><code>xxx</code> finally, the string to substitute</li> <li><code>/yyy/</code> replace the match with <code>yyy</code></li> <li><code>g</code> all occurences on the line</li> </ul> <p>This gives the same result as using the example in @Kent's answer, it will handle any order of <code>str1</code>, <code>str2</code>, <code>str3</code> and <code>xxx</code>.<br> I repeat that this is an alternative if for some reason a function is not an alternative. As I understand OP, e.g. <code>str2</code> is always after <code>str1</code> in his case, so this is more a general solution for situations like this (and perhaps more of academic interest).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload