JavaSE - day11 regular expression

Regular expressions exist in the class Pattern, which is a class in the java.util.regex package. It has no constructor, but contains fields and methods.

Why introduce regular expressions?

    For example, we need to limit the format of some strings, such as the length of the QQ number and cannot start with 0, such as the mobile phone number must start with 1 and the length must be 11 digits, such as email authentication must have a suffix, with @. If you use basic grammar to judge, it will be very troublesome, so regular expressions are introduced, so that complex judgments can be summarized in one sentence. This is the role of regular expressions.

Summary of Regular Expression Constructs

   
 
character
x character x
\\ backslash character
\0n character n with octal value 0 (0  <= n <=  7)  
\ 0 nn Character nn with octal value 0 (0  <= n <=  7)  
\0mnn Character mnn with octal value 0 (0  <= m <=  3  , 0 <= n <=  7)    
\x hh character hh with hex value  0x
\u hhhh character hhhh with hex value  0x
\t Tab character ( '\u0009' )
\n New line (newline) character ( '\u000A' )
\r carriage return ( '\u000D' )
\f Form feed ( '\u000C' )
\a Alarm (bell) character ( '\u0007' )
\e escape character ( '\u001B' )
\cx control character corresponding to x
 
character class
[abc] a , b or c (simple class)
[^abc] Any character except a , b or c (negation)
[a-zA-Z] a to z or A to Z , both letters inclusive (range)
[a-d[m-p]] a to d or m to p : [a-dm-p] (union)
[a-z&&[def]] d , e or f (intersection)
[a-z&&[^bc]] a to z , except b and c : [ad-z] (subtract)
[a-z&&[^m-p]] a to z , not m to p : [a-lq-z] (subtract)
 
predefined character classes
. any character ( may or may not match line terminator )
\d Numeric: [0-9]
\D Non-numeric: [^0-9]
\s Whitespace characters: [ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
 
POSIX 字符类(仅 US-ASCII)
\p{Lower} 小写字母字符:[a-z]
\p{Upper} 大写字母字符:[A-Z]
\p{ASCII} 所有 ASCII:[\x00-\x7F]
\p{Alpha} 字母字符:[\p{Lower}\p{Upper}]
\p{Digit} 十进制数字:[0-9]
\p{Alnum} 字母数字字符:[\p{Alpha}\p{Digit}]
\p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325814816&siteId=291194637