gpt4 book ai didi

java - 可用空间正则表达式选项 (Pattern.COMMENTS) 未按预期工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:02 26 4
gpt4 key购买 nike

我正在尝试使用正则表达式检测亵渎行为。但我想检测这个词,即使他们把“Profa nity”这样的词隔开。但是,当使用“(?x)”选项时,它仍然不想检测。

我目前得到:

(?ix).*Bad Word.*

我试过使用 http://www.rubular.com运气不好调试表达式。

如果对 Teamspeak Bot 有任何帮助,我想踢用户,因为他们的名字中有禁用词。在配置中它指的是 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html我找不到与 (?) 选项相关的任何内容。

机器人本身可以在这里找到:https://forum.teamspeak.com/threads/51286-JTS3ServerMod-Multifunction-TS3-Server-Bot-(Idle-Record-Away-Mute-Welcome-)

最佳答案

when using the "(?x)" option it still doesn't want to detect

(?x) 是一个嵌入式标志选项(也称为内联修饰符/选项)启用 Pattern.COMMENTS 选项,也称为自由间距在正则表达式中启用注释并使正则表达式引擎忽略模式中所有常规空白的模式。根据 Free-Spacing in Character Classes :

In free-spacing mode, whitespace between regular expression tokens is ignored. Whitespace includes spaces, tabs, and line breaks. Note that only whitespace between tokens is ignored. a b c is the same as abc in free-spacing mode. But \ d and \d are not the same. The former matches d, while the latter matches a digit. \d is a single regex token composed of a backslash and a "d". Breaking up the token with a space gives you an escaped space (which matches a space), and a literal "d".

Likewise, grouping modifiers cannot be broken up. (?>atomic) is the same as (?> ato mic ) and as ( ?>ato mic). They all match the same atomic group. They're not the same as (? >atomic). The latter is a syntax error. The ?> grouping modifier is a single element in the regex syntax, and must stay together. This is true for all such constructs, including lookaround, named groups, etc.

因此,要使用 (?x) 修饰符匹配模式中的单个空格,您需要将其转义:

String reg = "(?ix).*Bad\\ Word.*";   // Escaped space matches a space in free spacing mode
String reg = "(?ix).* Bad\\ Word .*"; // More formatting spaces, same pattern

注意不能将空格放入字符类中以使其在 Java 正则表达式中有意义。见下文:

Java, however, does not treat a character class as a single token in free-spacing mode. Java does ignore spaces, line breaks, and comments inside character classes. So in Java's free-spacing mode, [abc] is identical to [ a b c ].

此外,我认为您实际上想确保您的模式可以匹配可能包含换行符的完整字符串。这意味着,您需要 (?s)Pattern.DOTALL、修饰符:

String reg = "(?is).*Bad Word.*";

此外,要匹配任何空格,您可以依赖 \s:

String reg = "(?ix).*Bad\\sWord.*"; // To only match 1 whitespace
String reg = "(?ix).*Bad\\s+Word.*"; // To account for 1 or more whitespaces

关于java - 可用空间正则表达式选项 (Pattern.COMMENTS) 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51451596/

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