Using RegEx to replace specific text in string variable

Hi guys, I am trying to remove specific texts of different string variable. May I know how I can write the code to get the expected results? I have checked some posts and saw this function:

System.Text.RegularExpressions.Regex.Replace(MyStrVariable, “TextToReplace”)

For example, if the text I wanna replace is %EnvironVariable%, how can I put this inside the function? I tried using RegEx builder website and this (%EnvironVariable%)+ works on the website but I dunno how to put it inside UiPath.

Thankyou very much for your help!

1 Like

Hey @Komom

Try to use this:
System.Text.RegularExpressions.Regex.Replace(MyStrVariable, "%EnvironVariable%", "")

If you want to remove any text enclosed in “%”, you can use:

System.Text.RegularExpressions.Regex.Replace(MyStrVariable, "%.*?%", "")

Hi @Komom

Pls try below expression:
MyStrVariable = System.Text.RegularExpressions.Regex.Replace(MyStrVariable, “%EnvironVariable%”, “”)

Hi @Komom

As you can see, the minimum requirement is a inputString, regexPattern, and the replacementString

You can do the following:

outputString = System.Text.RegularExpressions.Regex.Replace(MyStrVariable, "%EnvironVariable%", replacementString)

Example:

inputString = "I am Roboto. Contact Me on 123456789 or roboto@gmail.com"
outputString = System.Text.RegularExpressions.Regex.Replace(inputString, "\d{9}", "PhoneNo")

Output:

If this solves your issue, Do mark it as a solution.
Happy Automation :star_struck:

1 Like

Thank you so much!! I feel like I have asked a dumb question haha

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.