Lua正则表达式(ASCII(0-127)字符) [英] Lua regex for ascii(0-127) characters

查看:163
本文介绍了Lua正则表达式(ASCII(0-127)字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LUA并尝试替换某些输入字符串中的非ascii字符.
我使用了 [^ \ x00– \ x7F] 正则表达式,但收到以下错误:
格式错误(缺少]")我也使用了 [^ [:ascii:]] ,但是它不起作用.
我认为 [^ \ x00– \ x7F] 是正确的方法,但我可能缺少一些转义.
请帮忙!

I am using LUA and trying to replace non ascii characters in some input string.
I used the [^\x00–\x7F] regex but getting the following error:
malformed pattern (missing ']') Also I used [^[:ascii:]] but it is does not work.
I think that [^\x00–\x7F] is the right way but I am probably missing some escape.
Please help!

推荐答案

要替换非ASCII字符:

To replace non-ascii characters:

string.gsub(your_string, "[\128-\255]", what_they_should_be_replaced_with)

替代方式:

string.gsub(your_string, "[^%z\1-\127]", what_they_should_be_replaced_with)

对于UTF-8文本:

string.gsub(your_string, "[\192-\255][\128-\191]*", what_they_should_be_replaced_with)

这篇关于Lua正则表达式(ASCII(0-127)字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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