\s
可以匹配空格。因此,正则表达式 \\s\\s
应该匹配两个空格。Pattern whitespace = Pattern.compile("\\s\\s");
matcher = whitespace.matcher(modLine);
while (matcher.find()) matcher.replaceAll(" ");
这是为了将所有连续的两个空格替换为一个空格。但实际上并不起作用。
我是否对正则表达式或“空格”一词有严重误解?
\s
可以匹配空格。因此,正则表达式 \\s\\s
应该匹配两个空格。Pattern whitespace = Pattern.compile("\\s\\s");
matcher = whitespace.matcher(modLine);
while (matcher.find()) matcher.replaceAll(" ");
在Java中,您不能单独使用\s
来匹配其本机字符集上的空格,因为Java不支持Unicode空格属性,尽管这样做严格要求满足UTS#18的RL1.2!。 Java所拥有的并不符合标准规范。
Unicode将26个代码点定义为\p{White_Space}
:其中20个是各种类型的\pZ
GeneralCategory = Separator,其余6个是\p{Cc}
GeneralCategory = Control。
空格是一种相当稳定的属性,这些相同的位置几乎已经存在了很长时间。即便如此,在Java中没有符合Unicode标准的该属性,所以您必须使用类似以下的代码:
String whitespace_chars = "" /* dummy empty string for homogeneity */
+ "\\u0009" // CHARACTER TABULATION
+ "\\u000A" // LINE FEED (LF)
+ "\\u000B" // LINE TABULATION
+ "\\u000C" // FORM FEED (FF)
+ "\\u000D" // CARRIAGE RETURN (CR)
+ "\\u0020" // SPACE
+ "\\u0085" // NEXT LINE (NEL)
+ "\\u00A0" // NO-BREAK SPACE
+ "\\u1680" // OGHAM SPACE MARK
+ "\\u180E" // MONGOLIAN VOWEL SEPARATOR
+ "\\u2000" // EN QUAD
+ "\\u2001" // EM QUAD
+ "\\u2002" // EN SPACE
+ "\\u2003" // EM SPACE
+ "\\u2004" // THREE-PER-EM SPACE
+ "\\u2005" // FOUR-PER-EM SPACE
+ "\\u2006" // SIX-PER-EM SPACE
+ "\\u2007" // FIGURE SPACE
+ "\\u2008" // PUNCTUATION SPACE
+ "\\u2009" // THIN SPACE
+ "\\u200A" // HAIR SPACE
+ "\\u2028" // LINE SEPARATOR
+ "\\u2029" // PARAGRAPH SEPARATOR
+ "\\u202F" // NARROW NO-BREAK SPACE
+ "\\u205F" // MEDIUM MATHEMATICAL SPACE
+ "\\u3000" // IDEOGRAPHIC SPACE
;
/* A \s that actually works for Java’s native character set: Unicode */
String whitespace_charclass = "[" + whitespace_chars + "]";
/* A \S that actually works for Java’s native character set: Unicode */
String not_whitespace_charclass = "[^" + whitespace_chars + "]";
现在你可以在replaceAll
中使用whitespace_charclass + "+"
作为模式。
很抱歉,Java的正则表达式在其本地字符集上表现不佳,因此您确实需要通过奇特的方法使它们起作用。
如果你觉得空格很烦人,你应该看看如何让\w
和\b
最终正常工作!
是的,这是可能的,但它真的很混乱。即使这样说也是太客气了。在Java中获得符合标准的正则表达式库的最简单方法是JNI到ICU的内容。这就是Google为Android所做的,因为OraSun的做法不够好。
如果您不想这样做,但仍然想坚持使用Java,我有一个前端正则表达式重写库,我编写了它来“修复”Java的模式,至少使它们符合UTS#18中RL1.2a的要求,即Unicode 正则表达式的要求。
\s
的更短方式是[\s\u0085\p{Z}]
。 - Robert Tupelo-Schneck是的,你需要获取matcher.replaceAll()
的结果:
String result = matcher.replaceAll(" ");
System.out.println(result);
对于Java(不是php,不是javascript,也不是其他任何语言):
txt.replaceAll("\\p{javaSpaceChar}{2,}"," ")
自这个问题被提出以来,Java已经发生了很大的变化。现在可以使用\p{Zs}
组匹配各种Unicode空格字符。
因此,如果你想用一个普通的空格替换一个或多个异国情调的空格,你可以这样做:
String txt = "whatever my string is";
String newTxt = txt.replaceAll("\\p{Zs}+", " ");
除此之外,如果你使用了trim()
字符串函数,你应该看一下相对较新的strip()
、stripLeading()
和stripTrailing()
字符串函数。它们可以帮助你去除各种奇怪的空白字符。有关包含哪些空格的更多信息,请参见Java的Character.isWhitespace()
函数。
\p{Zl}
作为行分隔符,\p{Zp}
作为段落分隔符。 - Roberttxt.replaceAll("(\\p{Zs}|\\p{Zl}|\\p{Zp})+", " ");
扩展为用单个空格字符替换所有分隔符。 - Robert[\s\p{z}]
排除了 Unicode 中的 "next line" 字符 U+0085。请使用 [\s\u0085\p{Z}]
。 - Robert Tupelo-Schneck对我来说似乎有效:
String s = " a b c";
System.out.println("\"" + s.replaceAll("\\s\\s", " ") + "\"");
将打印:
" a b c"
我认为您打算使用以下代码而不是您的代码:
Pattern whitespace = Pattern.compile("\\s\\s");
Matcher matcher = whitespace.matcher(s);
String result = "";
if (matcher.find()) {
result = matcher.replaceAll(" ");
}
System.out.println(result);
您可以使用以下代码片段实现您的目的:
import org.apache.commons.lang3.StringUtils;
StringUtils.normalizeSpace(string);
String sampleString = "Hello world!";
sampleString.replaceAll("\\s{2}", " "); // replaces exactly two consecutive spaces
sampleString.replaceAll("\\s{2,}", " "); // replaces two or more consecutive white spaces
。
Pattern whitespace = Pattern.compile("\\s", Pattern.UNICODE_CHARACTER_CLASS);
Pattern.UNICODE_CHARACTER_CLASS
选项“启用了预定义字符类和POSIX字符类的Unicode版本”,这些字符类“符合Unicode技术标准#18:Unicode正则表达式附录C:兼容属性”的规范。
相同的行为也可以通过(?U)
嵌入式标志表达式来启用。例如,如果您想在Java中使用正则表达式替换/删除所有Unicode空格,则可以使用
String result = text.replaceAll("(?U)\\s+", ""); // removes all whitespaces
String result = text.replaceAll("(?U)\\s", "-"); // replaces each single whitespace with -
String result = text.replaceAll("(?U)\\s+", "-"); // replaces chunks of one or more consecutive whitespaces with a single -
String result = text.replaceAll("(?U)\\G\\s", "-"); // replaces each single whitespace at the start of string with -
String text = "\u00A0 \u00A0\tStart reading\u00A0here..."; // \u00A0 - non-breaking space
System.out.println("Text: '" + text + "'"); // => Text: ' Start reading here...'
System.out.println(text.replaceAll("(?U)\\s+", "")); // => Startreadinghere...
System.out.println(text.replaceAll("(?U)\\s", "-")); // => ----Start-reading-here...
System.out.println(text.replaceAll("(?U)\\s+", "-")); // => -Start-reading-here...
System.out.println(text.replaceAll("(?U)\\G\\s", "-")); // => ----Start reading here...
Pattern whitespace = Pattern.compile("\\s\\s");
matcher = whitespace.matcher(modLine);
boolean flag = true;
while(flag)
{
//Update your original search text with the result of the replace
modLine = matcher.replaceAll(" ");
//reset matcher to look at this "new" text
matcher = whitespace.matcher(modLine);
//search again ... and if no match , set flag to false to exit, else run again
if(!matcher.find())
{
flag = false;
}
}
你可以使用更简单的方式:
String out = in.replaceAll(" {2}", " ");