任何字符,包括换行符 - Java Regex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3222649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:32:40  来源:igfitidea点击:

Any character including newline - Java Regex

javaregex

提问by Mick

I thought it may be [.\n]+ but that doesn't seem to work?

我认为它可能是 [.\n]+ 但这似乎不起作用?

采纳答案by Artefacto

The dot cannot be used inside character classes.

不能在字符类中使用点。

See the option Pattern.DOTALL.

请参阅选项Pattern.DOTALL

Pattern.DOTALLEnables dotall mode. In dotall mode, the expression .matches any character, including a line terminator. By default this expression does not match line terminators. Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)

Pattern.DOTALL启用 dotall 模式。在 dotall 模式下,表达式.匹配任何字符,包括行终止符。默认情况下,此表达式不匹配行终止符。Dotall 模式也可以通过嵌入的标志表达式启用(?s)。(s 是“单行”模式的助记符,这就是 Perl 中的名称。)

If you need it on just a portion of the regular expression, you use e.g. [\s\S].

如果您只需要在正则表达式的一部分上使用它,您可以使用 eg [\s\S]

回答by Jason L.

Edit: While my original answer is technically correct, as ThorSummoner pointed out, it can be done more efficiently like so

编辑:虽然我的原始答案在技术上是正确的,但正如 ThorSummoner 指出的那样,它可以像这样更有效地完成

[\s\S]

[\s\S]

as compared to (.|\n)or (.|\n|\r)

相比(.|\n)(.|\n|\r)