Find text in string and replace a part of it?

Hi guys, I’m trying to find a specific text in a string and then replace that text but maintain the integrity of the text around it, for example:

3 bdAaaaa aaaaa aaaa aa | aa aaa 3 bd| aaa aa aaal

I want to replace " bd[a-zA-Z]+" or without using regex any instance of " bd" followed directly with a letter without space in between, and replace it with " bd|" so the text above would be:

3 bd|Aaaaa aaaaa aaaa aa | aa aaa 3 bd| aaa aa aaal

I only know how to replace it so that the replacement string would begin as 3 bd|aaaa taking the “A” with it.

You dont need to open another thread :wink:

But to understand what you are asking now is that if an instance of “bd” is found you want to check if it is followed by a char (not whitespace), if true then replace it with “bd|”?

Yes, this is slightly different than the other thread. So for all instances in astring where " bd" is followed by a letter I want to replace " bd" with " bd|" while leaving the letters that originally followed it in the string intact.

So for example…

3 bd| aaa do nothing
3 bdAaaaa replace with 3 bd|Aaaaa OR 3 bd|aaaa (which one)

3 bd| aaa do nothing
3 bdAaaaa replace with 3 bd|Aaaaa NOT 3 bd|aaaa

So the first two are what I’m looking to do.

So there’s an additional step to do this outside of what I attached in the last one. This time after you split the string based on “bd”. Once its split you can run the String Array through a while loop.

Use an If Statement within the Loop that checks the starting char of the following string element char position 0. If it " " assign string = srtring + splitString.Element, if anything other than " " assign sting = string + “bd|” + splitString.Element

I have a couple questions on that, I’m still new to UiPath so I actually didn’t fully understand the flowchart that you had sent. So two questions are 1. Is there a way to just get the position of the text in the string when it matches? so like the function can output the position as the 3rd character in the string lets so so I can just extract characters 3 through 6 as a string and just replace them exactly but with a pipe character added in.

Also it sounds like your pretty familair with UiPath are there any activities to download that are specific to string and parsing related functions? It just seems like, again coming from a different rpa platform and still beginning on this one, that its unnecessarily complicated to manipulate strings in comparison to all the rest of the toolkit that UiPath offers.

You can search for custom packages here:

How would I go about doing this:

  • Find a string via regex
  • If found output the index of that string
  • Extract up to 5 characters starting with that index number as a substring
  • Replacing the original regex find with that substring
  • loop for all subsequent positive hits on that same regex

Regex: bd[a-zA-Z]{1}
String: aaaaaaabdaaaaa
Index of substring: 7
Should extract as substring: bdaaa (index positions 7-12)
Replacement string to put back: bd|aa
Original string after substring replacement: aaaaaaabd|aaaaa

Hey @css
I’m not sure I understand what you really want, but if you want to use Regex to replace a string where the string is in the pattern, then maybe like this:
System.Text.RegularExpressions.Regex.Replace(str, "bd", "bd|")
where str is the variable containing your text.

I was just keeping it simple. Feel free to let us know if you require more complex logic :smiley:

Regards

Hey @ClaytonM what I’m looking for would need something more complex. I’m looking for situations in a large string where " bd" is immediately followed by another letter (for example " bdA"), and I want to replace it with " bd|" so that in this example the text would read " bd|A". And do that for every instance where bd is immediately followed by another letter.

I can make regex to find those situations, but I don’t know how to replace it without, using the example above, removing the “A” character, which I need to leave intact.

What I’d ideally like to do is find the instances where " bd" is followed by a letter and get the index of that instance within the larger string, extract from the index of that instance to 5 characters and then I’d be able to easily replace those 5 characters with 6 new ones (the original 5 plus the “|” character after the " bd"). It is basically a formatting thing, where every incidence of " bd" should be immediately followed by a “|” character, but a couple of times for external reasons there are a few instances that don’t have that format.

So again, these are the steps I need.

  • Find a string via regex (" bd")
  • If found output the index of that string (lets say the regex finds it has an index of “7”)
  • Extract 5 characters starting with that index number as a substring (extract characters 7-12)
  • Replacing the original regex find with that substring (replace 7-12 with the same charcters only now a “|” character is added after " bd")
  • loop for all subsequent positive hits on that same regex (continue to look if other instances within the larger, original string)