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)
精彩评论