只有一个孤独插入符号的角色类怎么办?

Joe*_*ger 11 regex perl latex regexp-grammars

在尝试回答问题时,在找到特定字符时将文本写入新行,我使用了Regexp :: Grammars.它一直对我感兴趣,最后我有理由学习.我注意到作者的描述部分有一个LaTeX解析器(我是狂热的LaTeX用户,所以这让我很感兴趣)但是它有一个奇怪的构造在这里看到:

    <rule: Option>     [^][\$&%#_{}~^\s,]+

    <rule: Literal>    [^][\$&%#_{}~^\s]+
Run Code Online (Sandbox Code Playgroud)

什么的[^]字符类完成?

Gum*_*mbo 21

[^][…]是不是两类字符,但包含任何其他字符,除了只有一个字符类],[(参见特殊字符里面一个括号字符类):

但是,如果]是括号中的字符类的第一个(或第二个,如果第一个字符是插入符号)字符,则它不表示类的结尾(因为您不能有一个空类)并且被认为是可以在不转义的情况下匹配的字符集.

例子:

"+"   =~ /[+?*]/     #  Match, "+" in a character class is not special.
"\cH" =~ /[\b]/      #  Match, \b inside in a character class
                     #  is equivalent to a backspace.
"]"   =~ /[][]/      #  Match, as the character class contains.
                     #  both [ and ].
"[]"  =~ /[[]]/      #  Match, the pattern contains a character class
                     #  containing just ], and the character class is
                     #  followed by a ].
Run Code Online (Sandbox Code Playgroud)