gpt4 book ai didi

regex - 只有一个单独的插入符号的字符类有什么作用?

转载 作者:行者123 更新时间:2023-12-04 01:43:08 25 4
gpt4 key购买 nike

在试图回答这个问题时 Writing text into new line when a particular character is found , 我已聘用 Regexp::Grammars .它一直让我感兴趣,最后我有理由学习。我注意到作者的描述部分有一个 LaTeX 解析器(我是一个狂热的 LaTeX 用户,所以这让我很感兴趣)但它在这里看到了一个奇怪的结构:

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

<rule: Literal> [^][\$&%#_{}~^\s]+
[^]做什么的字符类完成?

最佳答案

[^][…]不是两个字符类,而是一个字符类,其中包含除 ] 之外的任何其他字符, [ , 和 (见 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 ].

关于regex - 只有一个单独的插入符号的字符类有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6332288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com