Hello, @jean-francois-trehard and All,
In wanting to explain, my regex S/R, provided in my previous post, I realized that we should add an hypothesis :
You must move the caret to a blank line, located before the text to be processed by the S/RSo, let’s start with the last version :
SEARCH (?:\$|\G)\w*?\K(?:(0)|(1)|(2))
REPLACE (?1G)(?2H)(?3I)
Globally, the regex (?:\$|\G)\w*?\K(?:(0)|(1)|(2)), searches, from after a literal $ symbol OR after the end of the previous search, if any, for the smallest range, even empty, of words characters, till a 0, 1 or 2 digit, then only selects this last digit and replaces it, respectively, with the G, H or I letters
Note that the \G assertion forces the regex engine to ask itself : does the current match attempt, immediately follows the previous match ? In case of a positive answer, the current match attempt is a possible match, so far !
As this regex only looks for consecutive words chars, it’s easy to understand why the characters, located, in the second part of a line, after the # sign, are not concerned !
Now, if you’re still in the fog, let’s go into a little more detail and start the search, for instance, with caret on an empty line, right before the first line dc.l $02220620 ; Tile #42
Beware, everything below is a bit “off-putting” but, in any case, it happens like that if you break the process down into smaller basic actions !
The regex engine first searches for a range of consecutive words characters till a 0, 1 or 2 digit, either, after a literal $ or after the end of the previous search. As no search has been performed, so far, \G syntax matches, by default, at current position, the beginning of the empty line, which is a zero-length string
As no word char exists in that empty line, the regex engine skips the EOL chars and moves to the beginning of the next line dc.l $02220620 ; Tile #42. You could say : it should match the dc string, which are word chars, also ? No, no ! Because the initial location, in the empty line, and the dc location are not contiguous. Indeed, there is a gap of the two chars : \r and \n )
Thus, the \G assertion is not true, presently. So the regex engine tries to match the first alternative and skips to the literal $ and the next 0 digit to search for
The \K feature cancels any match, so far and resets the regex engine working position => So, it only matches this first 0 digit. Remember that this configuration is possible as the range of chars before digit 0, 1 or 2 to search for, may be empty
As the group 1 is defined, when matching the 0 digit, the regex engine replaces it with the string G
Now, as no more $ symbol exists, the regex engine needs to use the second alternative, the \G assertion, which represents the location right between the letter G and the next range of word chars till a 0, 1 or 2 digit. So it just matches the first 2 digit, right after and, again, only selects that digit 2
As the group 3 is defined when matching the 2 digit, the regex engine replaces it with the string I
The second, third and fourth 2 digit, of current line, as well as the second 0 digit, are matched, in the same way as above, and replaced, consequently, with the appropriate letter
Then, the regex engine matches the 62 digit, right after the previous G letter, which verifies the \G assertion and selects only the fourth digit 2 of current line, due to the \K syntax
Again, this 2 digit is replaced with a I letter, as group 3 is defined when matching the 2 digit
The regex engine advances one position and matches the last 0 digit of current line and replaces it with the G letter
Now, things become interesting : the regex engine must find a next range of consecutive word chars, possibly empty, ending with, either, a 0, 1 or 2 digit. Obviously, this next range of contiguous word chars is the string 42, located some chars after the end of our previous search ! So the \G assertion is not verified anymore and, as there is no other $ symbol, either, the overall search fails. Thus, the regex engine skips the remaining chars of that first line, after the third 0 digit, which has been changed into G and moves to the beginning of the second line where the process resumes !
And, when a line just ends with a first range of word chars, without any comments zone, it, necessarily, will search for a literal $ symbol, as the \G feature cannot be true, due to the gap produced by the EOL characters of current line !
As you may notice, the key point, in this kind of data, is that, the several ranges of words characters are not juxtaposed. So, the \G assertion forces, automatically, the process to cancel any further search after examination of each first range of consecutive words chars of each line, following a $ symbol ;-))
Note also that, due to the \K syntax, inside this search regex, you cannot use a step by step replacement with several clicks on the Replace button. However, you can use the Find Next button, to get the different matches
Wow ! Glad to see that you’re still there, after these long explanations ;-)) Thank you for your patience and full attention !
Best Regards,
guy038