2

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

但是,要做到这一点,我需要每个字符类匹配的确切 ascii 代码。

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

4

1 回答 1

2

看看Lua 源代码

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 值范围。

于 2016-09-12T09:04:48.827 回答