cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to perform multiple search and replace in a string?

jfpelletier
Contributor

Hello all,

I sometimes have to do multiple search and replace operations on a string, and I'm wondering if there's a better way than using multiple ".replaceAll()" methods on that string? The patterns would be easier to maintain and update too if they were stored in a table rather than in a long string of ".replaceAll('this', 'that')"...

Thanks in advance for sharing your solutions!

JF

1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@jfpelletier - Please download the attached zip file and decompress it.  Import the SLP and upload the expr file to the same project via Manager.  Your regular expressions and replacement strings are in the expr file - note the subtle changes required to the regular expression with the XML tags.

Basically, I'm using the Expression Library to store the regex as an array, then using the Array.reduce() method to spin through that array and process each replacement on the input variable.

Please note that if the number of replacements is very large and the volume of data is also large, you may see a significant CPU load on your execution node.  Just something to keep in mind if you have a number of other jobs that need to run at the same time as this process.

Hope this helps!

View solution in original post

4 REPLIES 4

alchemiz
Contributor III

Hi @jfpelletier,
Good day, you can use regex pattern.
e.g.

alchemiz_0-1726724215358.png~EmEm 

jfpelletier
Contributor

Hello @alchemiz,

Thanks a lot for your suggestion, however I'm already using regex patterns (a few actually). More precisely, I'm looking for a way to maybe store the patterns in an associative array, and loop them all to apply them all on the same string. Having the patterns (regex and fixed strings) in an associative array would greatly help with maintenance when more patterns are required.

Example of what I mean (the real list is much longer):

{
"Jan." : "January",
"Feb." : "February",
"Mon." : "Monday",
"<sup>(.*)</sup>" : "\1",
"otherRegex" : "replace text"
}

Is there a way to store the strings to replace (including regex) in a structure as above and loop through it to have all the replacements done?

Thanks,

JF

koryknick
Employee
Employee

@jfpelletier - Please download the attached zip file and decompress it.  Import the SLP and upload the expr file to the same project via Manager.  Your regular expressions and replacement strings are in the expr file - note the subtle changes required to the regular expression with the XML tags.

Basically, I'm using the Expression Library to store the regex as an array, then using the Array.reduce() method to spin through that array and process each replacement on the input variable.

Please note that if the number of replacements is very large and the volume of data is also large, you may see a significant CPU load on your execution node.  Just something to keep in mind if you have a number of other jobs that need to run at the same time as this process.

Hope this helps!

Hello @koryknick,

Yes, that's exactly what I needed, it works perfectly for me!

Thanks a million and have a great day!

JF