Community
    • Login

    Search for 2 strings with AND not OR

    Scheduled Pinned Locked Moved General Discussion
    9 Posts 4 Posters 3.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • John CurreyJ
      John Currey
      last edited by

      Hi,

      I like to search files and find all files that contain line that have "def " AND “phone”.

      I tried searching using def |phone and I get all results where "def " or “phone are found”. I need only results where both exist on a line.

      Thanks.

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @John Currey
        last edited by

        @John-Currey

        Something like this could do it:

        Find what box: (?-is)^(?=.*def)(?=.*phone).+
        Search mode radiobutton: Regular expression

        Note that this works regardless of whether def or phone occurs first on the line.

        1 Reply Last reply Reply Quote 1
        • John CurreyJ
          John Currey
          last edited by

          @Alan-Kilborn said in Search for 2 strings with AND not OR:

          (?-is)^(?=.*def)(?=.*phone).+

          Hi Alan,

          Thanks for your response. I’m looking to search the entire content of the file. I want the results to show any file and any line in that file that contains both strings.

          I tried your suggestion with the radio button set to Regular Expression and only received 2 results. I’m certiain there are more.

          Thanks again.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @John Currey
            last edited by

            @John-Currey said in Search for 2 strings with AND not OR:

            I’m looking to search the entire content of the file. I want the results to show any file and any line in that file that contains both strings.

            These two things aren’t related to the “and” nature of your desired search.

            search the entire content of the file

            So use Find All in Current Document button to run the search?

            show any file and any line in that file

            So use Find All button on Find in Files tab to run the search?

            Your statements make me believe you don’t really know how to do searching in Notepad++, and go beyond the original question.

            Anyway, to show that what I provided does indeed work, I took this data:

            Alan
            Alan Carol
            Alan Bob
            Carol Alan Bob
            Bob
            Alice Ted Carol
            Ted Carol Alan
            Ted Carol Bob
            Alice Carol Bob
            Ted Carol
            Ted Alice
            Ted Bob
            Alice Alan Bob
            Alice
            Bob Alan Ted Alice
            Bob Carol
            Alice Carol Alan
            Carol
            Alan Ted
            Bob Alice
            Bob Alice Alan Ted Carol
            Bob Carol Ted Alice
            Alan Alice
            Alice Ted Alan
            Ted
            Ted Alan Bob
            Carol Alan Ted Alice
            Bob Carol Alan Alice
            Alice Ted Bob
            Bob Carol Alan Ted
            Carol Alice
            

            And I ran this search on it:

            Find what box: (?i-s)^(?=.*Bob)(?=.*Alan).+
            Search mode radiobutton: Regular expression
            Press the Find All in Current Document button

            Which is a search for “Bob and Alan on a line, in either order”.

            And I obtained these results:

            5db4ad99-7a3c-4f64-93d0-1f6f7b50eb29-image.png

            1 Reply Last reply Reply Quote 3
            • John CurreyJ
              John Currey
              last edited by

              Alan,

              I appreciate your assistance. As you’ll notice, the PHONELIB.WBS file contains both “DEF” and “PHONE” on lines 1 and 22 and is located in my search folder, yet the search string you provided did not find it as you can see from the results. Any thoughts on what I may have done wrong?

              Thanks.

              06f6e86e-7298-44c5-9288-172cba1ecf5c-image.png

              astrosofistaA 1 Reply Last reply Reply Quote 0
              • astrosofistaA
                astrosofista @John Currey
                last edited by

                Hi @John-Currey, @Alan-Kilborn, All:

                I think I understand where is the issue: you are searching in a non-insensitive mode (?-is), that is, in a sensitive way, when the correct mode is an insensitive mode, as (?i-s).

                HTH

                1 Reply Last reply Reply Quote 4
                • John CurreyJ
                  John Currey
                  last edited by

                  HTH,

                  Ahhhh, yes, that did the trick!

                  Thanks to you and Alan for the help.

                  1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn
                    last edited by Alan Kilborn

                    My bad. I should have stated the original search like this, I suppose:

                    Find what box: (?-s)^(?=.*def)(?=.*phone).+
                    Match case checkbox: unticked or ticked <------ as you need

                    Not everyone knows that:

                    • leading off your regex with a variant of (?i) will search case INSENSITIVELY (the i is for Insensitive)
                    • leading off your regex with a variant of (?-i) will search case SENSITIVELY (the -i is for NOT [or “minus”] Insensive)

                    and that these little strings, if used, override the setting of the Match case checkbox, since they are logically processed after the checkbox setting.

                    However, the misunderstanding could have been avoided had some sample data been provided along with the original question.

                    1 Reply Last reply Reply Quote 3
                    • Janica WilliamsJ
                      Janica Williams Banned
                      last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote -7
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors