View Single Post
08/17/15, 06:03 AM   #3
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
string.gsub has an optional max_replacements parameter, so atm I'm passing 1 to prevent multiple replacements.

Some more tests:
Lua Code:
  1. /script function gsubtest(pat, str) local i=0; local function repl(m) i=i+1; return string.format("(%d:%s)", i, m); end; df("gsub(%q, %q) -> %q", pat, str, string.gsub(pat, str, repl)); end
  2.  
  3. /script gsubtest("une rune de puissance", "^r*une")
  4. -> "(1:une) rune de puissance"
  5.  
  6. /script gsubtest("une rune de puissance", "^r*une ")
  7. -> "(1:une )(2:rune )de puissance"
  8.  
  9. /script gsubtest(" une rune de puissance", "^r*une ")
  10. -> " une rune de puissance"

Warning: Spoiler


It appears the anchor actually works at first. The 3rd example string starts with a space, and there's no match, which is correct. But in the 2nd example, after the first replacement (1:une ), the anchor incorrectly matches at "rune", as if the matcher was reset and thought it was at the start of the string.