Priyadarshini
Priyadarshini

Reputation: 129

Ant is not replacing multiple instances in the same line

I'm using replaceregexp to replace a particular match in files. Whenever there are multiple matches in a single line it is only replacing the last instance of the line.

Eg:
Input:
    f1.lblTest.text != null && f1.lblTest.text != ""
Reg EX:
 <replaceregexp file="mod1.js" match="(.*)f1(\.|\[&quot;)lblTest(\.|&quot;\]|\[&quot;)(.*)" replace="\1f1\2lblTestNew\3\4 \\/\\/Modified" flags="g">
Expected Output:
    f1.lblTestNew.text != null && f1.lblTestNew.text != "" //Modified
Actual Output:
    f1.lblTest.text != null && f1.lblTestNew.text != "" //Modified
With this <replaceregexp file="mod1.js" match="f1(\.|\[&quot;)$lblTest(\.|&quot;\]|\[&quot;)" replace="f1\1lblTestNew\2 \\/\\/Modified" flags="g"> I'm getting the below output
f1.lblTestNew. //Modifiedtext != null && f1.lblTestNew. //Modifiedtext != "".
 With this <replaceregexp file="mod1.js" match="f1(\.|\[&quot;)$lblTest(\.|&quot;\]|\[&quot;)" replace="f1\1lblTestNew\2" flags="g"> I'm getting the below output
f1.lblTestNew.text != null && f1.lblTestNew.text != "" This is ok but I really wanted a comment that lets the users know that this is modified and if they want they can modify again.

Please suggest any solutions.

Note:I tried g,m,s flags with and without 'byline' already and they didn't work.

Upvotes: 1

Views: 376

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626896

Since you have no conditional replacement pattern support in Ant replaceregexp, there is only one possible solution involving 2 steps:

  • <replaceregexp file="mod1.js" match="f1(\.|\[&quot;)$lblTest(\.|&quot;\]|\[&quot;)" replace="f1\1lblTestNew\2" flags="g"> to replace all the lblTest with lblTestNew texts
  • Append the //Modified substring to the end of the strings, say, with <replaceregexp file="mod1.js" match="(.*f1(\.|\[&quot;)lblTestNew(\.|&quot;\]|\[&quot;).*)" replace="\1 // Modified">.

Upvotes: 1

Related Questions