Home > Back-end >  How to replace a string pattern with increasing integers using regex
How to replace a string pattern with increasing integers using regex

Time:11-06

I have multiple .xml files in which I have a repeating string pattern. I want to replace each instance of that pattern with an integer starting with "1".

e.g.

Input:

APPLE is my favorite fruit. APPLE is red in color.

Expected Output:

1 is my favorite fruit. 2 is red in color.

CodePudding user response:

I am sure there are other extensions that can do this, but using one that I wrote this is very easy: enter image description here

CodePudding user response:

With Regex Text Generator you can do this.

  • select all the APPLE instances you want, any way you like, Ctrl D, Selet All Occurrences, Alt Enter from Find dialog
  • execute command Generate Text based on regular expression
  • as match expression use: .*
  • as generator expression use: {{=i 1}}
  • Related