Community
    • Login

    Replace text in search results?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    10 Posts 4 Posters 1.4k 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.
    • Steve Smith 0S
      Steve Smith 0
      last edited by

      In the find box, using “Find all in Current Document” displays those results in lines in “Search Results”. Is there any way to either directly edit these lines in the search results tab, and/or, find and replace text only within those search results?

      Terry RT Mark OlsonM 2 Replies Last reply Reply Quote 0
      • Terry RT
        Terry R @Steve Smith 0
        last edited by Terry R

        @Steve-Smith-0 said in Replace text in search results?:

        Is there any way to either directly edit these lines in the search results tab, and/or, find and replace text only within those search results?

        Not as you might think. However if you were to read the online manual, specifically the search result area here (see paragraph which includes …Double-clicking with the mouse…) you will see that it links to the main editor window and allows you to quickly move around the file/tab based on the search results to edit that text you were searching on.

        Terry

        1 Reply Last reply Reply Quote 2
        • Mark OlsonM
          Mark Olson @Steve Smith 0
          last edited by

          @Steve-Smith-0
          I don’t know what exactly you’re trying to do, but one generic way to find/replace only in lines that match some specification is to do regex-replace with positive forward lookahead from the start of each line for the first constraint.

          For example, if you want to find/replace all instances of the letter c with d on lines that contain ab, the regex/replace (?-si)^(?=.*ab).*\Kc for d
          would convert

          abc
          cab
          ccc
          dd ab c
          c ab
          

          to

          abd
          dab
          ccc
          dd ab d
          d ab
          
          1 Reply Last reply Reply Quote 1
          • Mark OlsonM
            Mark Olson
            last edited by

            My regex to find/replace c with d in lines containing ab was wrong, here’s the improved version:
            Replace (?-si)(?:^(?=.*ab)|(?!\A)\G).*?\Kc with d
            Should convert

            abc
            cab
            ccc
            z a c
            dd ab c
            c abcccc
            

            to

            abd
            dab
            ccc
            z a c
            dd ab d
            d abdddd
            

            whereas the regex in my previous post would only convert one c at a time from the last line.

            Jacob StephensonJ 1 Reply Last reply Reply Quote 2
            • Jacob StephensonJ
              Jacob Stephenson @Mark Olson
              last edited by

              @Mark-Olson user with very limited knowledge here - would this work with a string of text?

              I.e

              (?-si)(?:^(?=.LckEXPR_)|(?!\A\G).?\KX2

              to search for LckEXPR_ in lines and replace X2

              Currently my notepad is not recognising the function

              Terry RT 1 Reply Last reply Reply Quote 0
              • Terry RT
                Terry R @Jacob Stephenson
                last edited by

                @Jacob-Stephenson

                The previous posts were using search mode set to “regular expression”. However if what you are looking to replace is static text (does not change in any way) then when you use the Replace function just type in the text you are looking for into the Find What field and the Replace With field has the replacement text. Set the “Search Mode” to “Normal”.
                So:
                Find What:LckEXPR_
                Replace With:X2

                You can have the “Match Case” box ticked but I think given the string to find, that probably won’t be necessary.

                Terry

                Jacob StephensonJ 1 Reply Last reply Reply Quote 1
                • Jacob StephensonJ
                  Jacob Stephenson @Terry R
                  last edited by

                  @Terry-R

                  Apologies, no I’d be following the previous posts whereby I’m looking for lines containing “lckEXPR_” in the same way the previous example looked for “ab” and then looking to replace “x2” with “x3” in those lines using regular expression

                  Terry RT 1 Reply Last reply Reply Quote 0
                  • Terry RT
                    Terry R @Jacob Stephenson
                    last edited by

                    @Jacob-Stephenson

                    So first question are there more than 1 instance of the X2 on any line? The regex you refer to is designed to cater to that, but that makes it more complicated. The previous regex caters for ONLY 1 instance on each line. Of course even though only 1 instance, it can be run multiple times until no occurrences changed.

                    So back to the other question, did you set the search mode as I stated?

                    Terry

                    Jacob StephensonJ 1 Reply Last reply Reply Quote 1
                    • Jacob StephensonJ
                      Jacob Stephenson @Terry R
                      last edited by

                      @Terry-R

                      Hi Terry,

                      There are not more than one instance - however with a lack of understanding of regex , I’m trialling a roundabout way of bookmarking relevant lines in Notepad++, then cutting the selection , amending and pasting back into the dataset.

                      Far too much manual intervention but does the job until I can actually learn!
                      Thanks

                      Terry RT 1 Reply Last reply Reply Quote 0
                      • Terry RT
                        Terry R @Jacob Stephenson
                        last edited by Terry R

                        @Jacob-Stephenson
                        You have given up too early.

                        I’ll give you this on a platter:

                        Example (taken from earlier posts with your strings embedded on some lines)

                        abLckEXPR_cX2
                        cab
                        X2c LckEXPR_ cc
                        z aX2 cLckEXPR_
                        dd ab c
                        c abcccc
                        

                        Find What:(?-s)^(?=.*LckEXPR_).*\KX2
                        Replace With:X3
                        Copy the red text above exactly and insert in the appropriate field in the “Replace” function.
                        search mode as “regular expression”
                        Cursor in first position of file, click on “Replace All”

                        Replace All is required due to the use of the \K command.

                        Resulting file will be:

                        abLckEXPR_cX3
                        cab
                        X3c LckEXPR_ cc
                        z aX3 cLckEXPR_
                        dd ab c
                        c abcccc
                        

                        Terry

                        PS go to regex101 website where this regex and example are shown. Description on right might help you start to understand the code.

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