开发者

Java Regex: Match a character followed by whitespace?

This is driving me nuts... I have an input string like so:

String input = "T ";

And I'm trying to match and replace the string with something like so:

String output = input.replace("T\\s", "argggghhh");
System.out.println(output);  // expected: "argggghhh"
                             // actual: "T "

What am I do开发者_运维问答ing wrong? Why won't the \\s match the space?

Keep in mind I want to match multiple white space characters (\\s+), but I can't get this simple case to work :(.


Use replaceAll() instead of replace().

replace() does not use regular expressions.

See http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence) vs. http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜