Re: Regular Expressions: find_replace_limit doesn't make all replacements
- Posted by petelomax Feb 28, 2022
- 1193 views
Not sure I can be that much help here, but I suspect it is length 1 rather than spaces vs. tabs:
include std/regex.e regex pattern = regex:new( `\s+(\S+)` ) sequence string = " 0.013AUD 0.00% 0.000AUD Sell 42 1 4.379MAUD - -0.00AUD - Energy Minerals\n" sequence expctd = "~0.013AUD~0.00%~0.000AUD~Sell~42~1~4.379MAUD~-~-0.00AUD~-~Energy~Minerals\n" sequence result = regex:find_replace_limit( pattern, string, `~\1`, 20 ) sequence reslt2 = regex:find_replace_limit( pattern, result, `~\1`, 20 ) puts(1,string) puts(1,expctd) puts(1,result) puts(1,reslt2) ? equal( result, expctd ) ? equal( reslt2, expctd )
Running the above on https://tio.run/#euphoria4 gives me
0.013AUD 0.00% 0.000AUD Sell 42 1 4.379MAUD - -0.00AUD - Energy Minerals ~0.013AUD~0.00%~0.000AUD~Sell~42~1~4.379MAUD~-~-0.00AUD~-~Energy~Minerals ~0.013AUD~0.00%~0.000AUD~Sell~42~1 4.379MAUD~- -0.00AUD~- Energy~Minerals ~0.013AUD~0.00%~0.000AUD~Sell~42~1~4.379MAUD~-~-0.00AUD~-~Energy~Minerals 0 1
(The second shot works because you've got rid of all the length 1 previous substitutions, and that would still be true even if they were originally back-to-back)
As above, no expert here, but the code you are looking for might be in https://github.com/OpenEuphoria/euphoria/blob/99dff754918b9f66267b631d9c0be1d63c256d87/source/be_pcre.c right at the end,
start_from = ovector[rc] + rep_s->length;
might perhaps be missing a -1 [and don't blame me if that goes into an infinite loop/make sure you test with a trailing space on the substitution]
HTH, Pete