【问题标题】:How to search Unicode whitespace char in Lua如何在 Lua 中搜索 Unicode 空白字符
【发布时间】:2014-06-14 20:35:00
【问题描述】:

我想使用 Lua 在文件中搜索 Unicode whitespace chars。对于 ASCII,我们可以使用 %s 但我没有找到任何东西来搜索 Unicode 文件中的空白字符。

【问题讨论】:

  • Lua 5.1 及更早版本不支持 unicode 字符。
  • @hjpotter92 这样看是不是有点太消极了?存储/连接/转发工作正常,其他一切都可以构建,但不是内置的。

标签: unicode lua whitespace lua-patterns


【解决方案1】:

Lua 5.2 及更早版本几乎不支持 Unicode。

(即将到来的)Lua 5.3 提供了一个基本的 UTF-8 库。但是,它仍然不知道字符的含义(例如什么是空白字符)。在使用utf8.codes 迭代每个代码点后,您需要自己完成该部分。

--table to be filled 
local whitespace = {0x9, 0xA, 0xB, 0xC, 0xD, 0x20, 0x85, 0xA0, 0x1680, 0x2000, 0x2001}

local str = 'hello\u{2000}world\n'
for _, c in utf8.codes(str) do
    for _, v in ipairs(whitespace) do
        if c == v then
            print 'whitespace found'
        end
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多