2

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.

Community
  • 1
  • 1
151SoBad
  • 115
  • 9

2 Answers2

2

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

Scenario 1

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
  • | - or
  • TEXTAFTER$ - match TEXTAFTER at the end of a line.

Replacement pattern:

  • (?{1} - if Group 1 is matched, then
    • TextA - return TextA
    • : - else
    • TextB - replace with TextB
  • ) - end of the conditional replacement pattern.

enter image description here

Scenario 2

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 line
  • TEXTBEFORE - 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 chars
  • TEXTAFTER - some text at the...
  • $ - end of line.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Appreciate that. Unfortunately, I can't get that to work, I presume due to the special characters in TextA and TextB. My result is (where AA represents text, and "_AA --AA-AA-AA 10 "AA_" represents TextA): _AA --AA-AA-AA 10 "AA_**foo_abc** – 151SoBad Jun 20 '17 at 09:57
  • `-` is not a special char outside a character class. Please post a real life example. – Wiktor Stribiżew Jun 20 '17 at 10:02
  • Thanks for clarifying - that's unusual as to why it didn't work then. I should have posted a better example. – 151SoBad Jun 20 '17 at 10:05
1

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.

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22