Regular Expressions: find_replace_limit doesn't make all replacements
- Posted by abuaf Feb 26, 2022
- 1223 views
Minimal program below misses a few replacements after the 6th match, then matches a few after.
include dll.e include std/error.e include std/get.e include std/math.e include std/console.e include std/os.e include std/types.e include std/io.e --include machine.e include std/regex.e sequence fname="Y:\\temp\\ASX-screener\\2022-02-24\\9.txt" end if --object data = read_lines(fname) regex r = regex:new("\\s+(\\S+)", BSR_ANYCRLF ) tr(not regex(r), {"Error parsing regex; %s", error_message(r)}) integer fin=open(fname, "r") object in, formatted while 1 do in = gets(fin) if not sequence(in) then exit end if while 1 do --Must repeat replace to do missed replacements formatted=in in=regex:find_replace_limit(r, formatted, "~\\1", 20) if equal(formatted, in) then exit end if --until replace makes no changes (hack; shouldnt need this loop) exit --comment out this line to repeat the replacement as a cludge fix end while puts(1, in & '\n') end while
Text in file (\t tab seperated fields);
0.013AUD 0.00% 0.000AUD Sell 42 1 4.379MAU - -0.00AUD - Energy Minerals
Gives output;
~0.013AUD~0.00%~0.000AUD~Sell~42~1 4.379MAUD~- -0.00AUD~- Energy~Minerals
If I repeat the replacement, then the missing matches get done. No problem using super-sed 3.60 and PCRE mode.