Replace text in search results?
-
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?
-
@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
-
@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
withd
on lines that containab
, the regex/replace(?-si)^(?=.*ab).*\Kc
ford
would convertabc cab ccc dd ab c c ab
to
abd dab ccc dd ab d d ab
-
My regex to find/replace
c
withd
in lines containingab
was wrong, here’s the improved version:
Replace(?-si)(?:^(?=.*ab)|(?!\A)\G).*?\Kc
withd
Should convertabc 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. -
@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
-
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
-
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
-
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
-
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 -
@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.