Stop searching a line after one match



  • Hi, is there a way to tell NP++ to return a matching line no more than once in the search output? Many times, the string I search for appears multiple times in the same line and NP++ returns each occurrence as a separate result line, and that makes my search results explode with repeated lines.
    In other words: stop searching a line after first match.

    To illustrate, I would like to only see the first result here, where I searched for the word “point”.
    2021-05-21_081425.jpg

    I couldn’t find anything in regular expressions that might help with this, but I’m new at this so I don’t know.
    Any help would be appreciated, thanks!



  • @haz-it said in Stop searching a line after one match:

    I couldn’t find anything in regular expressions that might help with this

    Try this regular expression search:

    (?i-s)point.*

    in the search results it will highlight more than just the first point, but a line’s highlighting will begin with the first point, so that it is easy to locate visually (like normal).



  • Maybe this one is a bit better as it only highlights the exact search phrase (point or Point in the example case):

    (?i-s)^.*?\Kpoint



  • We can even do better:

    Start with some “pointy” text:

    the point I get your point but the point is
    only one point but
    this point is one 
    I have no p o i n t
    the point Point point Point is
    I'm a capital Point so...here's a point but
    what's the point
    point is
    zzzzpoint
    Point
    

    Run this search regex on it, using Find All in Current Document:

    (?i-s)^.*?\K(point)(?:.*\1)?

    to obtain:

    Imgur

    This search will output a line with one or more point / Point on it only ONCE. Additionally, on lines with multiple point / Point, the highlighted hit text will cover from the first point / Point to the last.

    BTW the hit text marking does not provide much contrast in N++ 8.0’s “dark mode” :-(
    So here’s what it looks like in non-dark mode:

    Imgur



  • The use of RegEx searches is all well-and-good for those expert with regular expressions, but for the rest of it requires us to remember complex strings (or remember where we stored a copy of such a string).
    It would be so much simpler if the maintainer(s) of Notepad++ could program the search facility with a user-selected (and saved) option that returns just one copy of each line containing multiple matches to the simple search string, as originally requested.
    I know I’m expecting a lot from a free program, but I’d be happy to make a donation if someone directed me to the ‘Donate’ page.
    Daniel
    (Australia)



  • @Daniel-Ford said in Stop searching a line after one match:

    The use of RegEx searches is all well-and-good for those expert with regular expressions, but for the rest of it requires us to remember complex strings (or remember where we stored a copy of such a string).

    So I presented some pretty advanced stuff, maybe, but really probably all that is needed in the usual case is to add a .* after your search term, example:

    point.*

    Is that all that hard to remember? I’d say No.

    It would be so much simpler if the maintainer(s) of Notepad++ could program the search facility with a user-selected (and saved) option that returns just one copy of each line containing multiple matches

    I suppose so, but that’s not what this Community forum is about.
    It’s about working with Notepad++ the way it is.
    That includes of course offering up workarounds, when mainstream behavior of the program doesn’t meet everyone’s need.

    I’d say for the majority of users, the current output is fine.
    But there’s always going to be people unhappy with fill-in-the-blank-here.



  • @Alan-Kilborn That’s… insane. You’re insane. Thanks a lot, it works great!

    It’s almost depressing for me because I know that I have zero chance of ever comprehending the syntax, so I’ll never be able to search the way I want, without asking for help. When I read the regex guide, I don’t even know the meaning of the words used there. It’s so beyond me, it’s like reading alien.

    @Daniel-Ford True. It would’ve been real nice to have a checkbox in the search dialog “Only search line until first match found” or some such.



  • @Alan-Kilborn I just noticed your edit. Yea if that’s the bare minimum needed, it’s not so bad to remember. Although I’d never have been able to get that from the guide which is in a foreign language to me.



  • @haz-it said in Stop searching a line after one match:

    That’s… insane. You’re insane.

    LOL. Well – thanks? I think? :-)

    if that’s the bare minimum needed, it’s not so bad to remember.

    You mean point.* I’m sure.

    Although I’d never have been able to get that from the guide which is in a foreign language to me.

    It’s not so bad. :-)
    Well for simple cases. :-)

    .* just means to match any character 0 or more times

    So point.* just starts at the first point on a line and anything else on the line is matched after it. This is key because to achieve one hit per line in the results, we want to include any possible following points into a single match.

    have a checkbox in the search dialog “Only search line until first match found”

    IIRC, this has been suggested and rejected in the past.
    Probably because a better solution would be to show multiple hits per output line.
    But that causes a complication because how would a user know, for longish lines, that there are/aren’t more hits offscreen to the right that they just can’t see?
    The current scheme doesn’t have that issue.



  • Just a final note that while this is fairly simple to remember:

    point.*

    it IS a bit under-specified.

    You’d want to make sure that . matches newline is unticked (probably not obvious) and if the intent is to match point and Point you (obviously) want to set the Match case checkbox appropriately.

    These settings were taken care of automatically with the longer expression of (?i-s)point.*, which overrides the settings for the checkboxes, but adding the (?i-s) is definitely a much harder thing to remember.



  • @Alan-Kilborn Thanks a lot, this is very useful and I also couldn’t figure it out.


Log in to reply