Lua模式替换大写字母

Lua模式替换大写字母,lua,pattern-matching,uppercase,lowercase,lua-patterns,Lua,Pattern Matching,Uppercase,Lowercase,Lua Patterns,我需要一个特殊的Lua模式,它将字符串中的所有大写字母替换为空格和相应的小写字母 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

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

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"))

%u
匹配大写字母
%l
对于小写=)@YuHao:您一定漏掉了一些东西(“…”),因为这并没有考虑增加的空间要求,实际上它与str:lower()没有什么不同。我只是给您另一种可能性。他可以保留奖杯:)
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