简体   繁体   English

为什么在我写replaceAll(“\\p{P}”,“”)时删除“\n”,因为“\\p{P}”只是PUNCTATION的正则表达式

[英]why is "\n" removed when I write replaceAll("\\p{P}", ""), because "\\p{P}" is a regular expression for PUNCTATION only

why is "\n" removed when I write replaceAll("\\p{P}", "") , because "\p{P}" is a regular expression for PUNCTATION only.为什么在我写replaceAll("\\p{P}", "")时删除了 "\n" ,因为 "\p{P}" 只是 PUNCTATION 的正则表达式。 "\n" line break is not a punctuation mark? "\n" 换行符不是标点符号吗?

For example, after this regular expression, a group of lines例如,在这个正则表达式之后,一组行

1,2,3
4.5
6.7?8

converted to 12345678转换为12345678

why was the line break removed?为什么要删除换行符?

The regex POSIX character class for puncutation ( \p{P} or \p{Punct} ) does not remove line-breaks.标点符号( \p{P}或 \p{ Punct \p{Punct} )的正则表达式POSIX 字符类不会删除换行符。

See the demo on IDEone :请参阅IDEone 上的演示

String multiline = "1,2,3\n4.5\n6.7?8";
System.out.println(multiline.replaceAll("\\p{P}", ""));

Prints the string with line-breaks preserved:打印保留换行符的字符串:

123
45
678

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM