Search for 2 strings with AND not OR



  • 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.



  • @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.



  • @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.



  • @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



  • 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



  • 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



  • HTH,

    Ahhhh, yes, that did the trick!

    Thanks to you and Alan for the help.



  • 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.


Log in to reply