gpt4 book ai didi

排除特定字符的正则表达式

转载 作者:行者123 更新时间:2023-12-03 13:19:24 33 4
gpt4 key购买 nike

this lesson我不明白为什么[^b]是不正确的?我明白 [^bog]是正确的。
[^b]应该匹配任何没有 b 的字符串字符并且不匹配包含任何 b 的任何字符串特点。

我的理解有什么问题吗?

最佳答案

对于那个特定的 lesson ,正确 regex是:

[^b]og

说明:
/[^b]og/

[^b] match a single character not present in the list below
b the literal character b (case sensitive)
og matches the characters og literally (case sensitive)

备注:

Negated Character Classes

Typing a caret after the opening square bracket negates the character class. The result is that the character class matches any character that is not in the character class. Unlike the dot, negated character classes also match (invisible) line break characters. If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.

It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead.

关于排除特定字符的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352038/

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