Java common objects 06

· Common Object regular expressions and other objects

Overview of regular expressions and simple to use

* A: the regular expression
  * refers to a string used to describe a single or series of line matching syntax rules Houge string. Is actually a rule, have their own special applications
  * effect: for example, registered mail, mail user name and password, usually limit the length of this thing is to limit the length of the regular expression to do

* B: Case presentation
  * Requirements:
  * Check qq number
  * 1, which calls for a 5--15 digits
  * 2, can not begin an 0
  * 3, must all be digital

  * A: Non-Regular Expression
  * b: Regular Expression

package com.heima.regex;

public class Demo1_Regex {
    
    public static void main(String[] args) {
        System.out.println(checkQQ("0123"));    // false
        System.out.println(checkQQ("a123456"));    // false
        System.out.println(checkQQ("123456"));    // true
        System.out.println(checkQQ("12345678910101010"));    // false
        System.out.println("-----------------------------");
        
        String regex = "[1-9]\\d{4,14}";
        System.out.println("A12345" .matches (regex));     // false 
        System.out.println ( "12345" .matches (regex));     // to true 
        System.out.println ( "012345" .matches (regex));     // to false 
        System.out.println ( "345" .matches (REGEX));     // to false 
        
    } 
    / * 
     * qq checksum analysis method number: 
     * 1, clear return value type: Boolean 
     * 2, clear the list of parameters: String qq 
     * / 
    public  static  Boolean checkQQ (String QQ) {
         Boolean flag = to true ; // If the check does not meet the requirements, put the flag is reset to flase, or direct return 

        IF (qq.length ()> =. 5 && qq.length () <= 15) {
             IF (! Qq.startsWith ( "0" )) {
                 char [] = ARR qq.toCharArray (); // convert the string to a character array 
                for ( int I = 0; I <arr.length; I ++ ) {
                     char CH = ARR [I]; // record each character 
                    IF (! (CH> = '0' && CH <= '. 9' )) { 
                        In Flag = to false ;    
                         BREAK ;     // if you do not meet the requirements, directly out 
                    } 
                } 
            } the else { 
                In Flag =to false ; // start with 0, does not meet the criteria registration number qq 
            } 
        } the else { 
            In Flag = to false ; // length does not fit 
        }
         return In Flag; 
    } 
}
Regex

 

 

Character class presentation

* A: character class
  * [abc]: a, b, or C (Simple)
  * [^ ABC]: any character, in addition to a, b or C (negative)
  * [A-zA-the Z]: A to z or is the a to Z, including two subtitles (range)
  * [0-9]: characters 0 through 9 are included

 

 

Package com.heima.regex; 


public  class Demo2_Regex { 

    / * 
     * stressed: the brackets represents a single character 
     * [abc] a, b, or C (Simple) 
     * [^ ABC] any character, in addition to a, b or C (Negative ) 
     * [a-zA-the z] z to a or a to z, including two letters (range) 
     * [AD [MP]] m to a or to d p: [a-dm-p ] ( and set) 
     * [AZ && [DEF]] D, E or f (intersection) 
     * [AZ && [^ BC]] A through z, except that b and c: [ad-z] (subtracting) 
     * [AZ && [^ MP] ] a through z, and not m through p: [a-lq-z ] ( subtracting) 
     * / 

    public  static  void main (String [] args) {
         // the demo1 ();
         // demo2 ();
         // Demo3 ();
         // demo4 ();
         // demo5();
        // demo6();
        // demo7();
        
    }


    public static void demo7() {
        String regex = "[a-z&&[^m-p]]";
        System.out.println("a".matches(regex));    // true
        System.out.println("m".matches(regex));    // false
        System.out.println("o".matches(regex));    // false
    }


    public static void demo6() {
        String regex = "[a-z&&[^bc]]";
        System.out.println("a".matches(regex));    // true
        System.out.println("b".matches(regex));    // false
        System.out.println("1".matches(regex));    // false
    }


    public static void demo5() {
        String regex = "[a-z&&[def]]";    // 取交集
        System.out.println("a".matches(regex));    // false
        System.out.println("d".matches(regex));    // true
    }


public static void demo4() {
    String reString = "[a-d[m-p]]";    // 等于[a-dm-p]
    System.out.println("a".matches(reString));    // true
    System.out.println("m".matches(reString));    // true
    System.out.println("e".matches(reString));    // false
    System.out.println("q".matches(reString));    // false
}

public static void demo3() {
    String regexString = "[a-zA-Z]"; //范围内的字符,头尾都包括在内
    System.out.println("a".matches(regexString));    // true
    System.out.println("z".matches(regexString));    // true
    System.out.println("A".matches(regexString));    // true
    System.out.println("Z".matches(regexString));    // true
    System.out.println("1".matches(regexString));    // false
}

public static void demo2() {
    String regex = "[^abc]"; // 除了a或b或c都行
    System.out.println("d".matches(regex));    // true
    System.out.println("a".matches(regex));    // false
    System.out.println("1".matches(regex));    // true
    System.out.println("%".matches(regex));    // true
    System.out.println("10".matches(regex));@ 10 represents a character and a character 0, a plurality of character -> to false 
    System.out.println ( "" The matches (REGEX).); // null string -> to false 
} 

    public  static  void the demo1 () { 
        String REGEX = "[ABC]"; // [] represents a single character, or b, or A represents a C 
        System.out.println ( "a" .matches (REGEX));     // to true 
        System.out.println ( "b" .matches (REGEX));     // to true 
        System.out.println ( "C" .matches (REGEX));     // to true 
        System.out.println ( "D" .matches (REGEX));     // to false 
    } 
}
Regex

 

Guess you like

Origin www.cnblogs.com/zhaochuming/p/12624648.html