Questions about Java regular matching whitespace characters (full-width spaces and half-width spaces)

Source: Internet
Author: User

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

    1. Half-width space ()
    2. Horizontal tab (\ t)
    3. Vertical tab
    4. Enter (\ r)
    5. line break (\ n)
    6. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.