Replace Function - How to replace two occurrence only not all

Hello All,

I am trying to replace string, only for two occurrence however i am unable to do it as it is replacing all the text that are matching.

could you please advise how we can able to replace only first two occurrence in text.


Hope the following sample helps you.

strData = "AbAbAb"
regex = New System.Text.RegularExpressions.Regex("A")
strData = regex.Replace(strData,"X",2)

Finally, you can get “XbXbAb” as result.
Note: regex is System.Text.RegularExpressions.Regex type variable.



Hello Thank you for providing me the solution.
will this code help me to do get the result in uipath??

Yes. You can do this using Assign activity. (In above case, it needs 3 Assign activities.) Can you try it?


1 Like

Hello Yoichi,

No, it is not working even if i take the assign activities it is giving me following error.

regex = New System.Text.RegularExpressions.Regex(“A”)

Cannot assign type reg to string


it is only taking two arguments not the 3rd one.



You need to define proper variable type in variable panel.

“regex” should be System.Text.RegularExpressions.Regex type.

Then, 2nd sentence should be the following

Str = regex.Replace(strData,"X",2)


1 Like

Yeaaah, it worked:) Thank you So much for your help. i appreciate

1 Like