regular expression to find lines containing multiple specific words or patterns in any arbitrary orderUsing a variable in a regex patternHow to replace specific lines with regex?Delete all of a file except for certain words that contain certain lettersHow to replace content between two patterns from the file?Find a non-regular expression tag in a specific tags fileSubstitute words enclosed between comma or open-parenthesis and comma or closed parenthesisHow to add quantifiers to group in a pattern in VIM?VIM regex - match all words equal to one under cursor, except one currently under the cursorreplace lines with the first and the last wordSwap grid[X][Y] to grid[Y][X] in full file

Simple Arithmetic Puzzle 7. Or is it?

Hotel booking: Why is Agoda much cheaper than booking.com?

List of lists elementwise greater/smaller than

How should I mix small caps with digits or symbols?

US F1 Visa grace period attending a conference

Don't understand notation of morphisms in Monoid definition

How to safely discharge oneself

tikz: 5 squares on a row, roman numbered 1 -> 5

Was murdering a slave illegal in American slavery, and if so, what punishments were given for it?

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

How did Arya and the Hound get into King's Landing so easily?

How to prove the emptiness of intersection of two context free languages is undecidable?

Warped chessboard

Farthing / Riding

Eigenvalues of the Laplace-Beltrami operator on a compact Riemannnian manifold

If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?

How to add a low pass filter to this non-inverting amplifier circuit?

What city and town structures are important in a low fantasy medieval world?

pwaS eht tirsf dna tasl setterl fo hace dorw

Why is this python script running in background consuming 100 % CPU?

Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?

Best practice for printing and evaluating formulas with the minimal coding

How can I use 400 ASA film in a Leica IIIf, which does not have options higher than 100?

Existence of a model of ZFC in which the natural numbers are really the natural numbers



regular expression to find lines containing multiple specific words or patterns in any arbitrary order


Using a variable in a regex patternHow to replace specific lines with regex?Delete all of a file except for certain words that contain certain lettersHow to replace content between two patterns from the file?Find a non-regular expression tag in a specific tags fileSubstitute words enclosed between comma or open-parenthesis and comma or closed parenthesisHow to add quantifiers to group in a pattern in VIM?VIM regex - match all words equal to one under cursor, except one currently under the cursorreplace lines with the first and the last wordSwap grid[X][Y] to grid[Y][X] in full file













6















Suppose we have the following very simple file:



1 x 3 x
x 3 x 1


Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter.



(Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)










share|improve this question







New contributor



myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    6















    Suppose we have the following very simple file:



    1 x 3 x
    x 3 x 1


    Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter.



    (Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)










    share|improve this question







    New contributor



    myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      6












      6








      6


      1






      Suppose we have the following very simple file:



      1 x 3 x
      x 3 x 1


      Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter.



      (Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)










      share|improve this question







      New contributor



      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Suppose we have the following very simple file:



      1 x 3 x
      x 3 x 1


      Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter.



      (Note that I used a very simple file on purpose. The file could contain many more lines, and the ”words“ could be words like ”hello“ and ”world“.)







      regular-expression






      share|improve this question







      New contributor



      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question







      New contributor



      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question






      New contributor



      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked May 14 at 7:26









      myrddmyrdd

      1636




      1636




      New contributor



      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          7














          Another way is to use :h /& , it works a bit like && in most coding language:



          pattern0&pattern1


          Above expression will match pattern1 if pattern0 also match at the same position.



          To solve your example:



          v^(.*1&.*3).*$



          • v very magic


          • ^ start of line


          • (.*1&.*3) match .*3 if .*1 matches.


          • .*$ match anything until line end

          If you have multiple patterns, you can write it as:



          v^(.*pattern0&.*pattern1&.*pattern2...).*$





          share|improve this answer




















          • 2





            I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

            – Rich
            May 14 at 9:52







          • 3





            This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

            – D. Ben Knoble
            May 14 at 17:11






          • 2





            it works a bit like && in most coding language: is brilliant!

            – Maxim Kim
            May 15 at 8:20


















          4














          Here's another alternative: use :h /bar to match either …1…3… or …3…1…:



          v.*(1.*3|3.*1).*



          • v very magic


          • .* anything (or nothing)


          • (...) a group, containing either:


            • 1.*3 1 followed by anything followed by 3


            • | or


            • 3.*1 3 followed by anything followed by 1



          • .* anything





          share|improve this answer

























          • nicely explained, therefore +1

            – myrdd
            May 15 at 8:12


















          3














          One option is to use a pattern with multiple lookaheads:



          magic: ^(.*1)@=(.*3)@=
          ‾ ‾

          very magic: v^(.*1)@=(.*3)@=
          ‾ ‾


          Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.




          additional hint:



          This even allows one to add another condition to the regular expression: matching lines that contain 1 and 3, but do not contain x:



          magic: ^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾

          very magic: v^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾





          share|improve this answer










          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1





            I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

            – Rich
            2 days ago











          • thank you @Rich for your edit and your suggestion!

            – myrdd
            21 hours ago











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "599"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          myrdd is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19945%2fregular-expression-to-find-lines-containing-multiple-specific-words-or-patterns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          Another way is to use :h /& , it works a bit like && in most coding language:



          pattern0&pattern1


          Above expression will match pattern1 if pattern0 also match at the same position.



          To solve your example:



          v^(.*1&.*3).*$



          • v very magic


          • ^ start of line


          • (.*1&.*3) match .*3 if .*1 matches.


          • .*$ match anything until line end

          If you have multiple patterns, you can write it as:



          v^(.*pattern0&.*pattern1&.*pattern2...).*$





          share|improve this answer




















          • 2





            I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

            – Rich
            May 14 at 9:52







          • 3





            This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

            – D. Ben Knoble
            May 14 at 17:11






          • 2





            it works a bit like && in most coding language: is brilliant!

            – Maxim Kim
            May 15 at 8:20















          7














          Another way is to use :h /& , it works a bit like && in most coding language:



          pattern0&pattern1


          Above expression will match pattern1 if pattern0 also match at the same position.



          To solve your example:



          v^(.*1&.*3).*$



          • v very magic


          • ^ start of line


          • (.*1&.*3) match .*3 if .*1 matches.


          • .*$ match anything until line end

          If you have multiple patterns, you can write it as:



          v^(.*pattern0&.*pattern1&.*pattern2...).*$





          share|improve this answer




















          • 2





            I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

            – Rich
            May 14 at 9:52







          • 3





            This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

            – D. Ben Knoble
            May 14 at 17:11






          • 2





            it works a bit like && in most coding language: is brilliant!

            – Maxim Kim
            May 15 at 8:20













          7












          7








          7







          Another way is to use :h /& , it works a bit like && in most coding language:



          pattern0&pattern1


          Above expression will match pattern1 if pattern0 also match at the same position.



          To solve your example:



          v^(.*1&.*3).*$



          • v very magic


          • ^ start of line


          • (.*1&.*3) match .*3 if .*1 matches.


          • .*$ match anything until line end

          If you have multiple patterns, you can write it as:



          v^(.*pattern0&.*pattern1&.*pattern2...).*$





          share|improve this answer















          Another way is to use :h /& , it works a bit like && in most coding language:



          pattern0&pattern1


          Above expression will match pattern1 if pattern0 also match at the same position.



          To solve your example:



          v^(.*1&.*3).*$



          • v very magic


          • ^ start of line


          • (.*1&.*3) match .*3 if .*1 matches.


          • .*$ match anything until line end

          If you have multiple patterns, you can write it as:



          v^(.*pattern0&.*pattern1&.*pattern2...).*$






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 14 at 8:14

























          answered May 14 at 8:05









          dedowsdidedowsdi

          1,182411




          1,182411







          • 2





            I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

            – Rich
            May 14 at 9:52







          • 3





            This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

            – D. Ben Knoble
            May 14 at 17:11






          • 2





            it works a bit like && in most coding language: is brilliant!

            – Maxim Kim
            May 15 at 8:20












          • 2





            I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

            – Rich
            May 14 at 9:52







          • 3





            This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

            – D. Ben Knoble
            May 14 at 17:11






          • 2





            it works a bit like && in most coding language: is brilliant!

            – Maxim Kim
            May 15 at 8:20







          2




          2





          I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

          – Rich
          May 14 at 9:52






          I've never really understood what /& is for. Now I do. Which is weird, because this is essentially identical to the "Peter and Bob" example given in the :help. Still, reading your answer, it finally clicked in my brain. Thanks! +1

          – Rich
          May 14 at 9:52





          3




          3





          This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

          – D. Ben Knoble
          May 14 at 17:11





          This is way easier to type in general, and I (like Rich) learned something. +1; this should be the accepted answer.

          – D. Ben Knoble
          May 14 at 17:11




          2




          2





          it works a bit like && in most coding language: is brilliant!

          – Maxim Kim
          May 15 at 8:20





          it works a bit like && in most coding language: is brilliant!

          – Maxim Kim
          May 15 at 8:20











          4














          Here's another alternative: use :h /bar to match either …1…3… or …3…1…:



          v.*(1.*3|3.*1).*



          • v very magic


          • .* anything (or nothing)


          • (...) a group, containing either:


            • 1.*3 1 followed by anything followed by 3


            • | or


            • 3.*1 3 followed by anything followed by 1



          • .* anything





          share|improve this answer

























          • nicely explained, therefore +1

            – myrdd
            May 15 at 8:12















          4














          Here's another alternative: use :h /bar to match either …1…3… or …3…1…:



          v.*(1.*3|3.*1).*



          • v very magic


          • .* anything (or nothing)


          • (...) a group, containing either:


            • 1.*3 1 followed by anything followed by 3


            • | or


            • 3.*1 3 followed by anything followed by 1



          • .* anything





          share|improve this answer

























          • nicely explained, therefore +1

            – myrdd
            May 15 at 8:12













          4












          4








          4







          Here's another alternative: use :h /bar to match either …1…3… or …3…1…:



          v.*(1.*3|3.*1).*



          • v very magic


          • .* anything (or nothing)


          • (...) a group, containing either:


            • 1.*3 1 followed by anything followed by 3


            • | or


            • 3.*1 3 followed by anything followed by 1



          • .* anything





          share|improve this answer















          Here's another alternative: use :h /bar to match either …1…3… or …3…1…:



          v.*(1.*3|3.*1).*



          • v very magic


          • .* anything (or nothing)


          • (...) a group, containing either:


            • 1.*3 1 followed by anything followed by 3


            • | or


            • 3.*1 3 followed by anything followed by 1



          • .* anything






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 14 at 9:47

























          answered May 14 at 9:41









          RichRich

          15.9k12066




          15.9k12066












          • nicely explained, therefore +1

            – myrdd
            May 15 at 8:12

















          • nicely explained, therefore +1

            – myrdd
            May 15 at 8:12
















          nicely explained, therefore +1

          – myrdd
          May 15 at 8:12





          nicely explained, therefore +1

          – myrdd
          May 15 at 8:12











          3














          One option is to use a pattern with multiple lookaheads:



          magic: ^(.*1)@=(.*3)@=
          ‾ ‾

          very magic: v^(.*1)@=(.*3)@=
          ‾ ‾


          Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.




          additional hint:



          This even allows one to add another condition to the regular expression: matching lines that contain 1 and 3, but do not contain x:



          magic: ^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾

          very magic: v^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾





          share|improve this answer










          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1





            I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

            – Rich
            2 days ago











          • thank you @Rich for your edit and your suggestion!

            – myrdd
            21 hours ago















          3














          One option is to use a pattern with multiple lookaheads:



          magic: ^(.*1)@=(.*3)@=
          ‾ ‾

          very magic: v^(.*1)@=(.*3)@=
          ‾ ‾


          Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.




          additional hint:



          This even allows one to add another condition to the regular expression: matching lines that contain 1 and 3, but do not contain x:



          magic: ^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾

          very magic: v^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾





          share|improve this answer










          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1





            I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

            – Rich
            2 days ago











          • thank you @Rich for your edit and your suggestion!

            – myrdd
            21 hours ago













          3












          3








          3







          One option is to use a pattern with multiple lookaheads:



          magic: ^(.*1)@=(.*3)@=
          ‾ ‾

          very magic: v^(.*1)@=(.*3)@=
          ‾ ‾


          Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.




          additional hint:



          This even allows one to add another condition to the regular expression: matching lines that contain 1 and 3, but do not contain x:



          magic: ^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾

          very magic: v^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾





          share|improve this answer










          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          One option is to use a pattern with multiple lookaheads:



          magic: ^(.*1)@=(.*3)@=
          ‾ ‾

          very magic: v^(.*1)@=(.*3)@=
          ‾ ‾


          Since lookaheads (and lookarounds in general) do not consume characters, the same line can be searched for multiple different patterns/words.




          additional hint:



          This even allows one to add another condition to the regular expression: matching lines that contain 1 and 3, but do not contain x:



          magic: ^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾

          very magic: v^(.*1)@=(.*3)@=(.*x)@!
          ‾ ‾ ‾






          share|improve this answer










          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.








          share|improve this answer



          share|improve this answer








          edited yesterday





















          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.








          answered May 14 at 7:26









          myrddmyrdd

          1636




          1636




          New contributor



          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




          New contributor




          myrdd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          • 1





            I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

            – Rich
            2 days ago











          • thank you @Rich for your edit and your suggestion!

            – myrdd
            21 hours ago












          • 1





            I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

            – Rich
            2 days ago











          • thank you @Rich for your edit and your suggestion!

            – myrdd
            21 hours ago







          1




          1





          I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

          – Rich
          2 days ago





          I'd consider removing the <, >, <, and > end-of-word atoms from this answer. They're not required for the simple example given in your question, and your regular expressions are easier to understand without them!

          – Rich
          2 days ago













          thank you @Rich for your edit and your suggestion!

          – myrdd
          21 hours ago





          thank you @Rich for your edit and your suggestion!

          – myrdd
          21 hours ago










          myrdd is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          myrdd is a new contributor. Be nice, and check out our Code of Conduct.












          myrdd is a new contributor. Be nice, and check out our Code of Conduct.











          myrdd is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Vi and Vim Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19945%2fregular-expression-to-find-lines-containing-multiple-specific-words-or-patterns%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?