Community
    • Login

    Stop searching a line after one match

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 4 Posters 2.6k 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.
    • Alan KilbornA
      Alan Kilborn @haz it
      last edited by Alan Kilborn

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

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

        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

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

          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

          haz itH 1 Reply Last reply Reply Quote 2
          • Daniel FordD
            Daniel Ford
            last edited by Daniel Ford

            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)

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Daniel Ford
              last edited by

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

              haz itH 1 Reply Last reply Reply Quote 0
              • haz itH
                haz it @Alan Kilborn
                last edited by haz it

                @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 KilbornA 1 Reply Last reply Reply Quote 1
                • haz itH
                  haz it @Alan Kilborn
                  last edited by

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

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

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

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

                      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.

                      R JR 1 Reply Last reply Reply Quote 1
                      • R JR
                        R J @Alan Kilborn
                        last edited by

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

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors