Java basics (regular expressions)

Basic grammar

1. Choose

| The vertical bar symbol represents selection (that is, union) and has the lowest priority. For example, "aaa | bbb" can match aaa or bbb.

2. Limited quantity

The quantity qualifier after a character is used to limit the number of characters allowed in the preceding character.

The most common quantity qualifiers include "+", "?" And "*" (unqualified quantity means it appears once and only once):

(1) The + sign means that the preceding character must appear at least once. (1 or more times).

        For example, "goo + gle" can match google, gooogle, goooogle, etc .;

 (2) ? The question mark indicates that the preceding character can only appear once at most. (0 times, or 1 time).

        For example: "colou? R" can match color or colour;

 (3) * The asterisk indicates that the preceding character may not appear, or may appear one or more times. (0 times, or 1 time, or multiple times).

        For example, "0 * 42" can match 42, 042, 0041, 00042, etc.

3. Expression rules

  (1)     Start position of matching string

(2)   $    matches the end position of the string

Case:

String s1 = "111223abc";
String regex = "^[0-9]+abc$";
boolean b = s1.matches(regex);
System.out.println(b);

(3) {n}  n is a non-negative integer. Match n times

     Example: o {2} can match food, not Bob.

(4) {n,}   n is a non-negative integer. Match n times

     Example: [o {2,}]

         Cannot match "o" in "Bob", but can match all o in "foooood".

        "o {1,}" is equivalent to "o +"

        "o {0,}" is equivalent to "o *".

(5) {n, m}   m and n are non-negative integers, where n <= m. Match at least n times and at most m times.

(6) x represents the character x

       Example: The matching rule is "a" , then the content of the string to be matched is "a".

(7) [abc]  character set, matching any one of the characters contained.

       Example: "[abc]" , the matched content is one of character a, or character b, or character c

Case: Match mobile phone number

/匹配手机号
//长度11位,开头为1,第二个数字是3、7、5、8
String regex4="1[3758][0-9]{9}";
Scanner sc=new Scanner(System.in);
System.out.println("请输入手机号:");
String number=sc.next();
boolean result=number.matches(regex4);
if(result==true)
     System.out.println("手机号匹配!");
else
     System.out.println("手机号不匹配!");

(8) [^ abc]   Excluded character set, which means any character except a , b or c

(9) [a-zA-Z]     matches a to z or A to Z , the two letters are included

 (10)   [0-9]   matches 0 to 9 digits, including the two digits

 (11)  [a-zA-Z_0-9] matches letters or numbers or underscores ( ie word characters )  

 (12) . (Dot) matches any single character except "\ n". If you want to use., Use the matching rule "\\." To achieve

(13) \ d    matches a numeric character. Equivalent to [0-9].

(14) \ w   matches any word character including underscores. Equivalent to "[A-Za-z0-9_]".

(15) \ b    matches a word boundary, that is, the position between the word and the space.

          For example, "er \ b" can match "er" in "never", but not "er" in "verb".

Case: Matching mailbox

String regex="[a-zA-Z_0-9]+@[a-zA-Z_0-9]+(\\.[a-zA-Z_0-9]+)+";
System.out.println("[email protected]".matches(regex));

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/93332102