Regex expressions in Java \\s vs.\\s+

来源:互联网 发布:凯立德端口查看 编辑:程序博客网 时间:2024/04/28 02:02

x = x.replaceAll("\\s", "");x = x.replaceAll("\\s+", "");

Those two replaceAll calls will always produce the same result, regardless of what x is. However, it is important to note that the two regular expressions are not the same:

  • \\s - matches single whitespace character
  • \\s+ - matches sequence of one or more whitespace characters.

In this case, it makes no difference, since you are replacing everything with an empty string (although it would be better to use \\s+ from an efficiency point of view). If you were replacing with a non-empty string, the two would behave differently.

0 0
原创粉丝点击