Lua模式替换大写字母

时间:2015-04-11 02:40:46

标签: lua pattern-matching uppercase lowercase lua-patterns

我需要一个特殊的Lua模式,它接受一个字符串中的所有大写字母,并用空格和相应的小写字母替换它们;

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

可以吗?

2 个答案:

答案 0 :(得分:3)

假设仅使用ASCII,则可以:

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

print(lowercase("TestStringOne"))
print(lowercase("this isA TestString"))

答案 1 :(得分:2)

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