等效于“[-x7FxC2-xF4][x80-xBF]*"的模式在 Lua 5.1 [英] Equivalent pattern to "[-x7FxC2-xF4][x80-xBF]*" in Lua 5.1

查看:17
本文介绍了等效于“[-x7FxC2-xF4][x80-xBF]*"的模式在 Lua 5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答这个问题时,我编写了这段代码来遍历字符串中的 UTF-8 字节序列:

When answering this question, I wrote this code to iterate over the UTF-8 byte sequence in a string:

local str = "KORYTNAČKA"
for c in str:gmatch("[-x7FxC2-xF4][x80-xBF]*") do 
    print(c) 
end

在Lua 5.2中可以使用,但是在Lua 5.1中,报错:

It works in Lua 5.2, but in Lua 5.1, it reports an error:

格式错误(缺少']')

malformed pattern (missing ']')

我记得在 Lua 5.1 中,不支持字符串文字 xhh,所以我将其修改为:

I recall in Lua 5.1, the string literal xhh is not supported, so I modified it to:

local str = "KORYTNAČKA"
for c in str:gmatch("[-127194-244][128-191]*") do 
    print(c) 
end

但错误依旧,如何解决?

But the error stays the same, how to fix it?

推荐答案

Lua 5.1 模式手册.

A pattern cannot contain embedded zeros. Use %z instead.

在 Lua 5.2 中,这已更改,以便您可以使用 代替,但在 5.1 中则不然.只需将 %z 添加到第一组并将第一个范围更改为 1-127.

In Lua 5.2, this was changed so that you could use instead, but not so for 5.1. Simply add %z to the first set and change the first range to 1-127.

这篇关于等效于“[-x7FxC2-xF4][x80-xBF]*"的模式在 Lua 5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆