Reputation: 125
To start off, I want to be able to do 2 things:
1st Thing:
To extract foo_abc (and similarly every other line, for example, goo_zxy, and doo_fgh), I needed to remove some text appended BEFORE foo_abc, and AFTER foo_abc.
For example:
TEXTBEFOREfoo_abcTEXTAFTER
TEXTBEFOREgoo_zxyTEXTAFTER
TEXTBEFOREdoo_fghTEXTAFTER
to obtain:
foo_abc
goo_zxy
doo_fgh
2nd Thing:
I now need to append different text before and after foo_abc again. Like so:
TextAfoo_abcTextB
So what I've done is:
Find: ^
Replace: TextA
Find: $
Replace: TextB
Which works well, but I have to perform a find&replace TWICE which is not very efficient. To avoid that, I found this: Multiple word search and replace in notepad++
And applied it like so:
Find: (^)|($)
Replace: (?1TextA)(?2TextB)
But it doesn't work out too well.
AND, as mentioned, I need this to work for EACH and every line: For example:
foo_abc
goo_zxy
doo_fgh
I need to insert TextA at the beginning for each of those lines, and TextB at the end of each line, like so:
TextAfoo_abcTextB
TextAgoo_zxyTextB
TextAdoo_fghTextB
Can this be done? (Yes, I actually need to do this to over 10000 lines, not just 3 and wanting an efficient way to do so).
Have I missed a quicker way to do all of this? Perhaps by performing a search and replace above in '1st Thing' on the TEXTBEFORE and TEXTAFTER, with TextA and TextB, respectively, in one-go?
Many thanks.
EDIT: Yes, they are literal strings. Yes, they do contain special characters because they are represent parts of a URL.
Upvotes: 2
Views: 2356
Reputation: 3161
Try:
TEXTBEFORE(.+?)TEXTAFTER
replace with
TextA$1TextB
See this for example and explanation
If you need to find whole line:
^TEXTBEFORE(.+?)TEXTAFTER$
Replace is the same as before.
Upvotes: 1
Reputation: 627469
There are two scenarios: 1) you want to replace the TEXTBEFORE
or TEXTAFTER
regardless of the fact that either of them exists, 2) both TEXTBEFORE
and TEXTAFTER
must exist
You may use a single search and replace operation for this:
Find What: ^(TEXTBEFORE)|TEXTAFTER$
Replace With: (?{1}TextA:TextB)
NOTE: If the TEXTBEFORE
and TEXTAFTER
contain special chars, you may use
Find What: ^(\QTEXTBEFORE\E)|\QTEXTAFTER\E$
Details:
^(TEXTBEFORE)
- match and capture into Group 1 TEXTBEFORE
at the start of a line|
- orTEXTAFTER$
- match TEXTAFTER
at the end of a line.Replacement pattern:
(?{1}
- if Group 1 is matched, then
TextA
- return TextA
:
- elseTextB
- replace with TextB
)
- end of the conditional replacement pattern.If you need to match lines starting with some text and ending with another, use
Find What: ^TEXTBEFORE(.*?)TEXTAFTER$
Replace With: TextA$1TextB
Details:
^
- start of a lineTEXTBEFORE
- some text here(.*?)
- Group 1 (that can be referred to with $1
backreference from the replacement pattern) matching any 0+ chars other than line break charsTEXTAFTER
- some text at the...$
- end of line.Upvotes: 2