Aashish Sharma
Aashish Sharma

Reputation: 27

Regular expression for duplicate string

Hello I am trying to formulate the regular expression to find substring and replace portion of that string. I have input in the format

Some_text_beginning_AASHISH_XX_YY_COPY_COPY_COPY_COPY

Please see that every string will have word AASHISH and in the end there could be indeterminate number of COPY. I want to delete all the COPY

I wrote the regular expression as

(.*)_AASHISH_(.*)_COPY+

I could find all the valid expression with this. But when I try to replace it with

$1_AASHISH_$2

It replaces just the last _COPY All the _COPY which came before last one are taken to be in group 2.

Further see that I am not using any programming language. I am using some third party tool. All it allows me is to search for string and replace it. It allows me to write regular expression.

Just to clarify why this question is not the same as posted before, tool I am using does not allow me use all regular expression somehow. I dont know how that tool is created. I just have UI. Thanks in advance

Upvotes: 1

Views: 189

Answers (1)

chuckx
chuckx

Reputation: 6877

Here's a regex that will capture the whole portion you want to maintain, resulting in a replacement that's just $1.

(.*_AASHISH_.*?)(?:_COPY)+

A few notes:

  • .*? - The ? on the end makes the repetition operator * non-greedy. It will match the minimum characters given its context.
  • (?:_COPY) - The ?: prefix makes this a non-capturing grouping.
  • + - The repetition operator will make the entire last group (_COPY) repeat 1 or more times, not just the Y.

Upvotes: 1

Related Questions