0

I have several of text files that contain the something like the following on many different lines:

this_is_THIS.doc

What I need to accomplish is to replace THIS with different objects for the first 5 occurrences and disregard the rest.

I would like for it to appear like the following:

this_is_TREE.doc
this_is_CAR.doc
this_is_CAT.doc
this_is_DONKEY.doc
this_is_ROCK.doc

I will have to do this many times in the future with the words changing so I feel a regex that I can alter in the future would help me a lot. I have searched but found nothing useful. Thanks for any help, you folks are great here.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
Bobby Peters
  • 181
  • 1
  • 1
  • 9
  • For such a job especially if the number of `THIS`is large, you should write a script in favorite scripting language. – Toto Dec 03 '17 at 11:19

1 Answers1

1

As long as you want to replace just 5 instances of THIS, I think the following solution is manageable. For this particular case, you can replace:

^(.+?)THIS(.+?)THIS(.+?)THIS(.+?)THIS(.+?)THIS

With

$1TREE$2CAR$3CAT$4DONKEY$5ROCK

Change the above texts like CAT, CAR as per your requirements.

Click for Demo

Before Replacing:

Don't forget to check . matches newline and Match case settings as shown below. enter image description here

After Replacing:

enter image description here

Note: Even I wouldn't recommend this method if you need to replace say 100 instances of THIS. The regex is going to be too long in that case.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43