使用转义(魔术)字符作为 Lua 模式中字符范围的边界
Using an escaped (magic) character as boundary in a character range in Lua patterns
第 6.4.1 节中关于 Lua 模式的 Lua manual 指出
A character class is used to represent a set of characters. The
following combinations are allowed in describing a character class:
x
: (where x
is not one of the magic characters ^$()%.[]*+-?
) represents the character x itself.
.
: (a dot) represents all characters.
%a
: represents all letters.
%c
: represents all control characters.
%d
: represents all digits.
%g
: represents all printable characters except space.
%l
: represents all lowercase letters.
%p
: represents all punctuation characters.
%s
: represents all space characters.
%u
: represents all uppercase letters.
%w
: represents all alphanumeric characters.
%x
: represents all hexadecimal digits.
%
x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
Any non-alphanumeric character (including all punctuation characters,
even the non-magical) can be preceded by a %
when used to represent
itself in a pattern.
[set]
: represents the class which is the union of all characters in set
. A range of characters can be specified by separating the end
characters of the range, in ascending order, with a -
. All classes
%x
described above can also be used as components in set. All other
characters in set
represent themselves. For example, [%w_]
(or
[_%w]
) represents all alphanumeric characters plus the underscore,
[0-7]
represents the octal digits, and [0-7%l%-]
represents the
octal digits plus the lowercase letters plus the -
character.
You can put a closing square bracket in a set by positioning it as the
first character in the set. You can put a hyphen in a set by
positioning it as the first or the last character in the set. (You can
also use an escape for both cases.)
The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
[^set]
: represents the complement of set, where set is interpreted
as above.
For all classes represented by single letters (%a
, %c
, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, %S represents all non-space characters.
The definitions of letter, space, and other character groups depend on
the current locale. In particular, the class [a-z]
may not be
equivalent to %l
.
(Highlighting and some formatting added by me)
那么,既然 "interaction between ranges and classes is not defined.",你如何创建一个字符 class set
开始 and/or 结束需要转义的(魔法)角色?
例如,
[%%-c]
没有定义范围从 %
到 c
的字符 class 并且包括中间的所有字符,而是仅包含三个字符的集合 %
、-
和c
。
The interaction between ranges and classes is not defined.
显然,这不是(一般正则表达式字符集的)硬性规定,而是 Lua 实施决定。虽然在字符 sets/ranges 中使用 shorthand 字符在某些(大多数)正则表达式风格中有效,但它并不完全有效(如在 Python 的 re 模块中,demo)。
然而,第二个例子具有误导性:
Therefore, patterns like [%a-z] or [a-%%] have no meaning.
虽然第一个示例很好,因为 %a
是 shorthand class(表示所有字母)在 set
中,但 [%a-z]
未定义如果与字符串匹配,将 return nil
。
[set]
中的转义范围字符
在第二个示例中,[a-%%]
、%%
只是定义了一个转义的 % 符号,而不是 shorthand 字符 class。表面上的问题是,范围是定义的颠倒,从高到低(参考US ASCII value of the characters a
61 and %
37), e.g like an erroneous Lua pattern like [f-a]
. If the set is defined in reverse order it seems to work: [%%-a]
but all it does is matching the three individual characters instead of the range of characters between %
and a
; credit cyclaminist).
这可以被认为是一个错误,实际上,如果需要转义定义范围字符之一,则无法在 [set]
中创建字符范围。
可能的解决方案
从下一个不需要转义的字符开始字符范围 - 然后单独添加剩余的转义字符,例如
[%%&-a]
for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
print(w)
end
这是我找到的答案。不过,也许其他人有更好的东西。
第 6.4.1 节中关于 Lua 模式的 Lua manual 指出
A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:
x
: (wherex
is not one of the magic characters^$()%.[]*+-?
) represents the character x itself..
: (a dot) represents all characters.%a
: represents all letters.%c
: represents all control characters.%d
: represents all digits.%g
: represents all printable characters except space.%l
: represents all lowercase letters.%p
: represents all punctuation characters.%s
: represents all space characters.%u
: represents all uppercase letters.%w
: represents all alphanumeric characters.%x
: represents all hexadecimal digits.%
x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters. Any non-alphanumeric character (including all punctuation characters, even the non-magical) can be preceded by a%
when used to represent itself in a pattern.[set]
: represents the class which is the union of all characters inset
. A range of characters can be specified by separating the end characters of the range, in ascending order, with a-
. All classes%x
described above can also be used as components in set. All other characters inset
represent themselves. For example,[%w_]
(or[_%w]
) represents all alphanumeric characters plus the underscore,[0-7]
represents the octal digits, and[0-7%l%-]
represents the octal digits plus the lowercase letters plus the-
character.You can put a closing square bracket in a set by positioning it as the first character in the set. You can put a hyphen in a set by positioning it as the first or the last character in the set. (You can also use an escape for both cases.)
The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
[^set]
: represents the complement of set, where set is interpreted as above.For all classes represented by single letters (
%a
,%c
, etc.), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.The definitions of letter, space, and other character groups depend on the current locale. In particular, the class
[a-z]
may not be equivalent to%l
.
(Highlighting and some formatting added by me)
那么,既然 "interaction between ranges and classes is not defined.",你如何创建一个字符 class set
开始 and/or 结束需要转义的(魔法)角色?
例如,
[%%-c]
没有定义范围从 %
到 c
的字符 class 并且包括中间的所有字符,而是仅包含三个字符的集合 %
、-
和c
。
The interaction between ranges and classes is not defined.
显然,这不是(一般正则表达式字符集的)硬性规定,而是 Lua 实施决定。虽然在字符 sets/ranges 中使用 shorthand 字符在某些(大多数)正则表达式风格中有效,但它并不完全有效(如在 Python 的 re 模块中,demo)。
然而,第二个例子具有误导性:
Therefore, patterns like [%a-z] or [a-%%] have no meaning.
虽然第一个示例很好,因为 %a
是 shorthand class(表示所有字母)在 set
中,但 [%a-z]
未定义如果与字符串匹配,将 return nil
。
[set]
中的转义范围字符
在第二个示例中,[a-%%]
、%%
只是定义了一个转义的 % 符号,而不是 shorthand 字符 class。表面上的问题是,范围是定义的颠倒,从高到低(参考US ASCII value of the characters a
61 and %
37), e.g like an erroneous Lua pattern like [f-a]
. If the set is defined in reverse order it seems to work: [%%-a]
but all it does is matching the three individual characters instead of the range of characters between %
and a
; credit cyclaminist).
这可以被认为是一个错误,实际上,如果需要转义定义范围字符之一,则无法在 [set]
中创建字符范围。
可能的解决方案
从下一个不需要转义的字符开始字符范围 - 然后单独添加剩余的转义字符,例如
[%%&-a]
for w in string.gmatch("%&*()-0Aa", "[%%&-a]") do
print(w)
end
这是我找到的答案。不过,也许其他人有更好的东西。