gpt4 book ai didi

java - 使用 Java 的正则表达式中的 Pattern.UNIX_LINES

转载 作者:行者123 更新时间:2023-12-02 05:07:25 26 4
gpt4 key购买 nike

您好,这里的 java 文档如下:

UNIX_LINES

public static final int UNIX_LINES

Enables Unix lines mode.

In this mode, only the '\n' line terminator is recognized in the behavior of ., ^, and $.

Unix lines mode can also be enabled via the embedded flag expression (?d).

有人有其他词来定义它的服务吗?我了解到“\n”转义序列仅在 .^$ 之后被识别。显然我被误解了。

最佳答案

我将尝试在 . 上解释它,因为同样的规则适用于 ^$

通常点 . 匹配除了新行之外的所有字符。在 Unix 中只有 \n 是换行符,所以回车 \r 等其他字符作为普通字符被威胁。

看看这个字符串 "A\r\nB\rC\nD"。如果您将尝试使用

.+ 之类的正则表达式找到匹配项
String data = "A\r\nB\rC\nD";
System.out.println(data);
Matcher m = Pattern.compile(".+").matcher(data);
while (m.find()) {
System.out.println("["+m.group()+"]");
}

你会得到

[A]
[B]
[C]
[D]

但如果添加标志 Pattern.UNIX_LINES 字符,如 \r 也可能匹配 . 并且输出将变为

[A
]
[B
C]
[D]

所以第一个匹配是[A\r],第二个是[B\rC],第三个是[C]

关于java - 使用 Java 的正则表达式中的 Pattern.UNIX_LINES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064527/

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