Today encountered a string, how to match the space is not successful!!!
I copied the space to the test.properties file to show "\u3000", what is this? This is the full-width space!!!
Check it out. \s does not support full-width
1. What kind of space does "\s" match?
\s matches any whitespace character in regular expressions, including spaces, tabs, page breaks, and so on; Chinese full-width spaces
\s does not match Chinese full-width spaces.
\s can only match the following six characters (see Java.util.regex.Pattern API DOC):
- Half-width space ()
- Horizontal tab (\ t)
- Vertical tab
- Enter (\ r)
- line break (\ n)
- Page Break (\f)
Escape \\s in Java code
Regular matches 0 or more spaces, I wrote this \\s*
Regular matches one or more spaces, I wrote this \\s+
2. How to match full-width spaces
The regular can be written like this: "[\\s\\p{zs}]+", note the escape in Java code.
3. Testing
Examples of my tests:
Package Com.test;public class Zhengze {/** * replaces multiple <br> into a single <br> * @param args */public static void main (string[] args) {String str1= "... In the middle omit a lot of ...<br> <br> think oneself boil peanut soup so simple ~ Hurry to try! <br><br> <br> <br> <br> "; String pattern = "<br> (\\s*<br>) +"; SYSTEM.OUT.PRINTLN ("The result of the \\s* substitution:"); System.out.println (Str1.replaceall (Pattern, "<br>")); String str2= "... In the middle omit a lot of ...<br> <br> think oneself boil peanut soup so simple ~ Hurry to try! <br><br> <br> <br> <br> "; String New_pattern = "<br> ([\\s\\p{zs}]*<br>) +"; System.out.println ("[Result of \\s\\p{zs}]* replacement:"); System.out.println (Str2.replaceall (New_pattern, "<br>"));}}
Results:
Results of \s* substitution: ... In the middle omit a lot of ...<br> <br> think oneself boil peanut soup so simple ~ Hurry to try! <br> <br> <br>[\s\p{zs}]* Replacement results: ... In the middle omit a lot of ...<br> think oneself boil peanut soup so simple ~ Hurry hands to try! <br>
Questions about Java regular matching whitespace characters (full-width spaces and half-width spaces)