Find all lines that contain specific strings



  • Hello everyone
    I have text file and I would like to search for lines that has specific partial strings and shows only the lines that has those strings

    Example:

    Please help me in this issue
    Thanks a lot for great help
    I receive help in this forum in great way. Please have a look
    You are great people
    Help me please
    in this line help is only exists
    these are some lines as an example
    these lines have to be searched by any string
    no case sensitive is required in search process

    I need to search for “pleas” and “help”


    The expected lines that would be displayed :

    Please help me in this issue
    I receive help in this forum in great way. Please have a look
    Help me please



  • @Yasser-Khalil

    A good way to get an answer would be to click here, but…

    …you can do it by searching this way:

    • Find-what box: (?-s)(?i)pleas.*help|help.*pleas
    • Search mode: Regular expression
    • Press Find All in Current Document button

    At that point your results will be shown in the Find result panel.



  • @Scott-Sumner said:

    (?-s)(?i)pleas.*help|help.*pleas

    Thank you very much scott for this great help
    But what if I need to search four or five strings … that would be confusing!!



  • @Yasser-Khalil said:

    search four or five strings

    Not so hard, one of the hits in the “here” link above explains it, but here is the basics of multiple-words-on-a-line-in-any-order searching:

    • 3 words: (?-s)(?=.*word1)(?=.*word2)(?=.*word3).*
    • 4 words: (?-s)(?=.*word1)(?=.*word2)(?=.*word3)(?=.*word4).*
    • 5 words: (?-s)(?=.*word1)(?=.*word2)(?=.*word3)(?=.*word4)(?=.*word5).*
    • etc.

    You can add \b before and after each word in your search if you truly want that exact word…and not words embedded in others…for example \btest\b will match test but not tester, testing, stoutest, etc.

    If this (or ANY posting on the Notepad++ Community site) is useful, don’t reply with a “thanks”, simply up-vote ( click the ^ in the ^ 0 v area on the right ).



  • Thank you very much for great and awesome help
    Best Regards



  • Hello, @yasser-hhalil and @scott-sumner,

    Sorry, I’m not very reactive, cause I’m on holidays, in North Brittany, with my two sisters and their husbands. What to say ? We make a cure of seafood : cockles, periwinkles, clams, lobsters, crabs and even octopus ! To sum up : the Paradise ;-))Since one week, weather was quite fine, here, but today it was raining. So, I’m back to N++ and the Community, for a while !!


    Ah, @scott-sumner, very clever regex, found by Alin Purcaru, on Stackoverflow site !

    To complete this topic, let’s suppose we want to find all lines with the five first-names : jack, james, jason, jules and Joe

    Here are, below, four regexes, from the more restrictive behaviour to the less restrictive behaviour :

    • In regex A, rhe five first-names must be true words, in the exact lower case

    • In regex B, rhe five first-names must be true words, whatever their case

    • In regex C, rhe five first-names may be glued in bigger words, but with their exact lower case

    • In regex D, rhe five first-names may be glued in bigger words, whatever their case

    So :

    • Regex A = (?-is)^(?=.*\bjack\b)(?=.*\bjames\b)(?=.*\bjason\b)(?=.*\bjules\b)(?=.*\bjoe\b).*

    • Regex B = (?i-s)^(?=.*\bjack\b)(?=.*\bjames\b)(?=.*\bjason\b)(?=.*\bjules\b)(?=.*\bjoe\b).*

    • Regex C = (?-is)^(?=.*jack)(?=.*james)(?=.*jason)(?=.*jules)(?=.*joe).*

    • Regex D = (?i-s)^(?=.*jack)(?=.*james)(?=.*jason)(?=.*jules)(?=.*joe).*

    Notes :

    • As usual, the Regular expression search mode must be checked !

    • The initial modifiers force the search to be sensitive / insensitive ( -i / i ) to case, and also means that dot matches a single standard character only ( -s )

    • The ^ assertion, stands for the location beginning of line, where starts the test of the following features ( look-arounds )

    • Each form (?.*......) is a positive look-around, a condition which must be verified to get an overall match

    • If all conditions are true, the final regex is simply ^.*, so all contents of the line, from its beginning


    Then, if you copy/paste the following lines, in a new tab :

    01 james
    02 jason
    03 joe
    04 james and jack
    05 jason and jules
    06 jack, jason and joe
    07 peter, joe, jack, james and jules
    08 peter, jack, james, jason, jules and joe
    09 james, joe, jason, jules, jack and margaret
    10 peter, jules, james, jack, jason, joe and margaret
    11 joe, jules, jason, james and jack
    
    12 jAMes
    13 jASon
    14 jOE
    15 jAMes and JAck
    16 jASon and JUles
    17 jACk, jASOn and joe
    18 pETer, JOE, jack, jaMEs and JUles
    19 pETer, JACk, james, JAson, jULes and joe
    20 jAMes, JOE, jason, jULes, jaCK and margaret
    21 pETer, JULes, james, Jack, jASon, jOE and margaret
    22 jOE, juLES, jason, jAMes and Jack
    
    23 james
    24 jason
    25 joe
    26 james and jack
    27 jason and jules
    28 jack, jason and joe
    29 peter, 12joe34, jack, 56james78 and jules
    30 peter, 12jack34, james, 56jason78, jules and joe
    31 james, 12joe34, jason, 56jules78, jack and margaret
    32 peter, 12jules34, james, 56jack78, jason, joe and margaret
    33 joe, 12jules34, jason, 56james78 and jack
    
    34 jAMes
    35 jASon
    36 jOE
    37 jAMes and 12jack34
    38 jASon and jules
    39 jACk, 12jASOn34 and joe
    40 pETer, 12JOE34, jack, 56jaMEs78 and JUles
    41 pETer, 12JACk34, james, 56JAson78, jULes and 90joe12
    42 jAMes, 12JOE34, jason, 56jULes78, jaCK and margaret
    43 pETer, 12JULes34, james, 56Jack78, jASon, 90joe12 and margaret
    44 jOE, 12juLES34, jason, 56jAMes78 and 90Jack12
    

    You’ll easily notice that :

    • Regex A matches lines from 8 to 11

    • Regex B matches lines from 8 to 11 and from 19 to 22

    • Regex C matches lines from 8 to 11 and from 30 to 33

    • Regex D matches lines from 8 to 11, from 19 to 22, from 30 to 33 and from 41 to 44


    Now, from an already saved file, once you’ve got the results, in the Find Result panel, after clicking on the Find All in Current Document button, using one of regexes, above, you may, also, search for each individual first-name :

    • Right-click in the Find result panel

    • Select the Find in this finder… option

    Now, in the new Find in finder dialog :

    • Type a first-name, for instance jack, in the Find what: zone

    • Check the Search only on found lines option

    • Uncheck the two options Match whole word only and Match case

    • Select, if necessary, the Normal search mode

    • Click on the Find all button

    => A new Find resul tab is created and displays any occurrence of the string jack, found in the lines of the Find result panel, only !

    • Repeat these steps, changing for an other first-name, to get 5 Find resul tabs, all using the Line Filter Mode of search !

    IMPORTANT : If the Find result panel contains results, from a non-saved file ( with new # name ), the context option Find in this filder… does NOT seem to work ! I’ll add a post to @don-ho, to that purpose, very soon !

    Best Regards,

    guy038


Log in to reply