只有一个插入符的字符类有什么作用?

发布于 2024-11-15 08:10:58 字数 478 浏览 5 评论 0原文

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

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

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

What do the [^] 字符类完成?

In trying to answer the question Writing text into new line when a particular character is found, I have employed Regexp::Grammars. It has long interested me and finally I had reason to learn. I noticed that the description section the author has a LaTeX parser (I am an avid LaTeX user, so this interested me) but it has one odd construct seen here:

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

    <rule: Literal>    [^][\
amp;%#_{}~^\s]+

What do the [^] character classes accomplish?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-11-22 08:10:58

[^][…] 不是两个字符类,而只是包含除 ][之外的任何其他字符的一个字符类… (参见特殊a 内的字符括号内的字符类):

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

示例:

"+" =~ /[+?*]/ # 匹配,字符类中的“+”并不特殊。
"\cH" =~ /[\b]/ # 匹配,字符类中的 \b
                     # 相当于退格键。
"]" =~ /[][]/ # 匹配,如字符类包含的那样。
                     # [ 和 ] 两者。
"[]" =~ /[[]]/ # 匹配,模式包含字符类
                     # 仅包含 ],字符类为
                     # 后跟一个 ]。

[^][…] is not two character classes but just one character class containing any other character except ], [, and (see Special Characters Inside a Bracketed Character Class):

However, if the ] is the first (or the second if the first character is a caret) character of a bracketed character class, it does not denote the end of the class (as you cannot have an empty class) and is considered part of the set of characters that can be matched without escaping.

Examples:

"+"   =~ /[+?*]/     #  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 ].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文