Lua 模式替换大写字母

Lua pattern replace uppercase letters

我需要一个特殊的 Lua 模式,它将字符串中的所有大写字母替换为 space 和相应的小写字母;

TestStringOne => test string one
this isA TestString => this is a test string

可以做到吗?

假设仅使用 ASCII,这有效:

function lowercase(str)
  return (str:gsub("%u", function(c) return ' ' .. c:lower() end))
end

print(lowercase("TestStringOne"))
print(lowercase("this isA TestString"))
function my(s)
  s = s:gsub('(%S)(%u)', '%1 %2'):lower()
  return s
end

print(my('TestStringOne'))              -->test string one
print(my('this isA TestString'))        -->this is a test string