A regular expression is a template for matching strings, which matches a batch of strings, so creating a regular expression is creating a special string.
a special character in a regular expression
| Metacharacters |
Description |
| |
matches any single character. For example, the regular expression " b.g " can match the following string: " big bug b g "but does not match" buug ". |
| $ |
matches line terminator. For example, the regular expression " ejb$ " i like EJB j2ee without EJBs ! ”。 |
| ^ |
^spring " can match string " spring is a the start of the Java EE framework i use Spring in my project ". |
| * |
match 0 zo* " can match " z " " zoo .* |
| / |
escape character, which is used to match metacharacters as normal characters. For example, the regular expression /$ Span style= "font-size:9pt" >/. |
| [] |
matches any one of the characters in the parentheses. For example, the regular expression "b[aui]gthe matchBug,Bigand theBug, but does not matchBeg. You can use hyphens in parentheses "-"To specify a range of characters to simplify the representation, such as regular expressions[0-9]you can match any numeric character so that the regular expression "A[]c"We can match"a0c","A1c","a2csuch as String, you can also create multiple intervals, such as "[A-za-z]"can match any uppercase and lowercase letters. There is also a meta-character that is used together "^"It's not like the one in front of you."^"means that the match line starts, but"Exclude"To match a character other than the specified interval, you can use it between the left parenthesis and the first character .^characters, such as "[^163a-z]"will be able to even match except1,6,3and any characters except for all uppercase letters. |
| () |
will () " Group group ) This meta-character is useful when extracting strings. |
| | |
Two matching criteria are logically " " operation. ' Z|food ' " Z "  or " Food " " Zood "  or " food " |
| + |
matches the preceding subexpression one or more times. For example regular expressions 9+ match 9 99 999 |
| |
matches the preceding sub-expression 0 or one time. For example, "do (es)?" "do"   "does"   "do"   |
| { n } |
match determined n times. For example, " e{2} " cannot match " bed " d seed e |
| { n ,} |
matches at least n times. For example, " e{2,} " does not match " bed " e " seeeeeeeed e |
| { n , m } |
Minimum Match n times and matches up to m times. "e{1,3}" will matchthefirst three "e" in "seeeeeeeed". |
general usage of regular Expressions
Once you have defined regular expressions in your program, you can use the pattern and matcher to work with regular expressions. Demo download
The pattern object is an in-memory representation of a regular expression after it is compiled, so the regular expression string must first be compiled into a pattern object, and then the pattern object is used to create the corresponding Matcher object. The States involved in performing a match remain in the Matcher object, and multiple Matcher objects can share the same pattern object.
Compiles a string into a pattern object
Pattern p = pattern.compile ("A*b");
To create a Matcher object using the Pattern object
Matcher m = P.matcher ("Aaaab");
Boolean B = m.matches (); Returns True
The pattern objects defined above can be reused multiple times. If a regular expression needs to be used only once, you can directly use the static matches method of the pattern class, which automatically compiles the specified string into an anonymous pattern object and performs a match as follows:
Boolean B = pattern.matches ("A*b", "Aaaab");
The above statement is equivalent to the previous three statements, but using such a statement requires recompiling the new pattern object and cannot reuse the compiled pattern object, so it is inefficient.
Pattern is an immutable class that can be used safely by multiple concurrent threads.
The Matcher class provides several common methods.
Find (): Returns whether the target string contains substrings that match the pattern.
Group (): Returns the last substring matching the pattern
Start (): Returns the start position of the last substring matching the pattern in the target string
End (): Returns the last substring matching the pattern at the end of the target string plus 1
Lookingat (): Returns whether the front part of the target string matches the pattern
Matches (): Returns whether the entire target string matches the pattern
Reset (): Applies an existing Matcher object to a character sequence of one type
Iii. Common Regular Expressions
Chinese match: [\\u4e00-\\u9fff]+
Mailbox match: \\w{3,20}@\\w+\\. (com|org|cn|net|gov)
Decimal match: \\d+\\.\\d+
ID number match: ^ (\\d{15}$|\\d{18}$|\\d{17} (\\D|X|X) $) $
On the Java regular expression