精确的Ascii代码与Lua模式匹配

时间:2016-09-12 07:50:18

标签: regex lua lua-patterns character-class

我正在将项目从Lua翻译成C ++。在Lua版本中,我使用Lua的正则表达式,但是出于一个如此简单的目的,在C ++中我可以通过简单地将字符与一些Ascii代码进行比较来实现。

然而,要做到这一点,我需要每个character class匹配的确切ascii代码。

例如,%s匹配所有空格字符,但这些字符究竟是什么?我需要知道每个Lua角色类。

1 个答案:

答案 0 :(得分:2)

查看Lua source

case 'a' : res = isalpha(c); break;
case 'c' : res = iscntrl(c); break;
case 'd' : res = isdigit(c); break;
case 'g' : res = isgraph(c); break;
case 'l' : res = islower(c); break;
case 'p' : res = ispunct(c); break;
case 's' : res = isspace(c); break;
case 'u' : res = isupper(c); break;
case 'w' : res = isalnum(c); break;
case 'x' : res = isxdigit(c); break;
case 'z' : res = (c == 0); break;  /* deprecated option */

您可以看到C++ <cctype> (ctype.h)中有类似的方法:

isalnum     Check if character is alphanumeric (function )
isalpha     Check if character is alphabetic (function )
isblank     Check if character is blank (function )
iscntrl     Check if character is a control character (function )
isdigit     Check if character is decimal digit (function )
isgraph     Check if character has graphical representation (function )
islower     Check if character is lowercase letter (function )
isprint     Check if character is printable (function )
ispunct     Check if character is a punctuation character (function )
isspace     Check if character is a white-space (function )
isupper     Check if character is uppercase letter (function )
isxdigit    Check if character is hexadecimal digit (function )

该页面上还有相应的ASCII值范围。