Remove Single instance of a string from a larger string

Hey guys, I haven’t been able to find a good answer for what I’m trying to do:
I have a large string that can contain anywhere from 6-18 lines of text. What I want to do is replace a single line in that larger string; sometimes the line I want to replace will appear in the larger string more than once, and it will typically never be in the same position (ie. it won’t always be the first line in the larger string or second line, ect.). Otherwise I want to keep the larger string intact, just have that one line of text removed from it, I iterate over this in my bot.

I’ve tried using regex but if there are duplicates this will remove multiple lines, I’ve also tried splitting before and after the line of text but again if there are duplicates this seems to return a blank value when splitting. Is there any easy way to tell regex or any replace function to only replace a single or first instance? Or use substring/ joins to do this?

Hi @cssc,

Use Regex you can easily remove that line but to identify the single line what you looking for you need to use Lookup pattern in ur regex

eg let say your line start with the word of “document the line you need to identify” just you have to use document key word as lookup value

Eg: Regex (?<=document)(Pattern for your line )

Happy Automation

1 Like

Hello

Further to @SamanGuruge’s post, its possible with Regex :slight_smile:

Please provide us with:

  • Post a sample or two.
  • Highlight/bold the content you want to remove.
  • Tell us about the pattern / provide us the information on the pattern.

Cheers

Steve

@cssc you can use the regex.replace(string,string,int32) method. Since you only want one instance replaced, the int32 would be 1

1 Like

0001|AAAA|9876|0393
0045|AABA|9A22|0393
0001|ABBA|9876|0393
0001|ABBA|9876|0393
5201|ADAA|0076|0223

Above is an example and in the bot that value will be contained inside of a string. What I want to do is replace one instance of “0001|ABBA|9876|0393” with “”. I need the rest of the string left intact including the second instance of the string that I am removing.

Also the string I’m trying to replace will not always be in the same positions each time. For example it could be this as my starting point:

0001|AAAA|9876|0393
0001|ABBA|9876|0393
0045|AABA|9A22|0393
0001|ABBA|9876|0393
5201|ADAA|0076|0223

1 Like

Hello

Is it always “0001|ABBA|9876|0393”? Or does it change?

It changes each time.

I basically have the line I want and I’m matching it to the larger string, if a match is found I need to remove that match from the larger string but only a single instance of that match because there will be another line I have that will match with, in this case, the second instance of that same string.

1 Like

You know what, I was just thinking I could just count the number of instances of the line in the larger string prior to regex-ing it out and then just add back more instances of that same line into the larger string, but that kind of seems like a dumb workaround for something there’s got to be a simple streamlined solution to.

Hi,

Can you try the following?

r = new System.Text.RegularExpressions.Regex("0001\|ABBA\|9876\|0393")
text = r.Replace(text,"",1)

note: r is System.Text.RegularExpressions.Regex type.

Regards,

1 Like

@cssc - why not use the regex.replace method I mentioned above? It does exactly what you’re looking to do

Your code can’t work in my version of UiPath, maybe it works on an updated version. I’m getting errors where Uipath:

  • doesn’t except ‘regex.replace’ without a preceding ‘system.text.regularexpressions’
  • needs a replacement text input
  • doesn’t accept a integer value of how many to replace

Thanks again Yoichi, this code did work.

1 Like

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