简体   繁体   中英

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 doing 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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