任何字符,包括换行符 - 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
Any character including newline - Java Regex
提问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.DOTALL
Enables 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)