It is currently June 26th, 2019, 12:42 am

Regex replace in one pass

Help with creating, editing & fixing problems with skins
Yincognito
Posts: 652
Joined: February 27th, 2015, 2:38 pm

Regex replace in one pass

Yincognito » March 11th, 2019, 10:42 pm

Suppose I have a string like: jsmorley is a smart guy (see what I just did here? :sly: ) in a measure, and I want to replace every letter with its alphabetical index (e.g. a->1, b->2 and so on), I would do like this:

Code: Select all

RegExpSubstitute=1
Substitute="a":"1","b":"2",...and so forth
...but this is running through the string 26 times (once for every letter in the alphabet) just to replace a single letter globally (in this case), which might be overkill from a performance point of view if you have to do a lot of (different) replacements. Is there a way to do this in a single regex pass through the whole string? That is, running through the string a single time, and replace letters as the regex advances through the string. Like I think it seems to be done here, in the first piece of code (this is in Python, not in C++ though).

In other words, is there a way to "concatenate" or "join" multiple regex substitutions in a single big regex substitution that achieves the same result? If not, is this an easy to implement thing in a potentially new option in Rainmeter measures?