Java Basics _ Regular expression

Source: Internet
Author: User

When making an account decision, it's a bit of a hassle if you judge each other.

Single character judgment

String string1 = "[ABC]"; The brackets represent a single character and cannot match the string System.out.println ("a". Matches (string1)); TRUESYSTEM.OUT.PRINTLN ("AB". Matches (string1)); falsestring string2 = "[^abc]";//^ is to take the anti-SYSTEM.OUT.PRINTLN ("a". Matches (string2));//falsesystem.out.println ("D"). Matches (string2))//truestring string3= "[a-za-z]";//a-z 26*2 characters System.out.println ("a". Matches (string3));// TrueSystem.out.println ("DC". Matches (String3));//falsestring string4= "[a-c[m-z]]";//a-z 26*2 characters System.out.println ("a". Matches (String4));//truesystem.out.println ("D". Matches (String4));//falsestring string5= "[A-z&&[def ]] "; the intersection of//A-Z and Def, namely DefSystem.out.println (" a ". Matches (String5));//falsesystem.out.println (" D ". Matches (STRING5)) ;//truestring string6= "[A-z&&[^def]]";//a-z and Def intersect, that is, except DefSystem.out.println ("a". Matches (String6));// TrueSystem.out.println ("D". Matches (String6));//false

Pre-defined character judgments

. Represents any character, one point represents any character, and two points represent two. and [] difference, [] no matter how much is a character string string1= "..."; System.out.println ("AA". Matches (string1));//truesystem.out.println ("?". Matches (string1));//false//  \d represents a single number of 0-9, but \ is an escape character, so two string string2= "\\d" is required; System.out.println ("Ten". Matches (string2));//falsesystem.out.println ("3". Matches (string2));//true//  \ D represents a single character string string3= "\\d" except for 0-9; System.out.println ("a". Matches (String3));//truesystem.out.println ("3". Matches (String3));//false//  \s denotes whitespace characters  \t\n\xob\f\rstring string4= "\\s"; System.out.println ("". Matches (String4));//truesystem.out.println ("3". Matches (String4));//false//  \s denotes non-whitespace characters  \t\n\xob\f\rstring string5= "\\s"; System.out.println ("". Matches (String5));//falsesystem.out.println ("3". Matches (String5));//true//  \w means [ A-ZA-Z0-9]. Uppercase W does not demonstrate string string6= "\\s"; System.out.println ("". Matches (String6));//falsesystem.out.println ("3". Matches (String6));//true

Number of times to judge

The representative does not appear once or once, and [] with [] is ABC for single character one or one does not occur string string1= "[ABC]?"; System.out.println ("a". Matches (string1));//truesystem.out.println ("". Matches (string1));// TrueSystem.out.println ("". Matches (string1));//false//* represents 0 times to several occurrences, where strings can be judged by string string2= "[abc]*]; System.out.println ("AAAAA". Matches (string2));//true System.out.println ("". Matches (string2));// TrueSystem.out.println ("ABCD". Matches (string2));//FALSESYSTEM.OUT.PRINTLN ("abc". Matches (string2));//true//+ Represents 1 times to several occurrences, you can judge string string3= "[abc]+"; System.out.println ("AAAAA". Matches (String3));//truesystem.out.println ("". Matches (string3));// FalseSystem.out.println ("ABCD". Matches (String3));//FALSESYSTEM.OUT.PRINTLN ("abc". Matches (String3));//true// {n} represents exactly n times, {n,m}, at least n times not more than M, and {n,}, at least n times string string4= "[Abc]{3}"; SYSTEM.OUT.PRINTLN ("abc". Matches (String4));//truesystem.out.println ("a". Matches (String4));// FalseSystem.out.println ("ABCD". Matches (String4));//false

String cutting

Java Basics _ Regular expression

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.