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.

6
  • Are TEXTBEFORE and TEXTAFTER literal strings? Try ^(TEXTBEFORE)|TEXTAFTER$ and replace with (?{1}TextA:TextB) Commented Jun 20, 2017 at 9:41
  • Question. Is the text you want to keep fixed, or is the before&after text you want to replace fixed?
    – LukStorms
    Commented Jun 20, 2017 at 9:43
  • Please, show us real cases. Are all foo_abc allways 3 lowercase letters, underscore, 3 lowercase letters?
    – Toto
    Commented Jun 20, 2017 at 9:46
  • @WiktorStribiżew My apologies, yes they are literal strings.
    – 151SoBad
    Commented Jun 20, 2017 at 9:48
  • @LukStorms Yes they are.
    – 151SoBad
    Commented Jun 20, 2017 at 9:49

2 Answers 2

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.
3
  • 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 "AAfoo_abc
    – 151SoBad
    Commented Jun 20, 2017 at 9:57
  • - is not a special char outside a character class. Please post a real life example. Commented Jun 20, 2017 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
    Commented Jun 20, 2017 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.

3
  • Works very well - even with special characters. Thank you.
    – 151SoBad
    Commented Jun 20, 2017 at 10:01
  • However, it does not find text at the start/end of a line. See my update. Commented Jun 20, 2017 at 10:02
  • @WiktorStribiżew Thanks for noticing - appreciate that.
    – 151SoBad
    Commented Jun 20, 2017 at 10:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.