Basic Java---Regular expressions

Source: Internet
Author: User
Tags alphabetic character

Regular expressions in many languages, such as Perl, PHP, Python, JavaScript, and JScript, support the processing of text with regular expressions, and some text editors implement advanced "search-and-replace" functionality with regular expressions.

A regular expression is a specification that can be used for pattern matching and substitution, and a regular expression is a text pattern consisting of ordinary characters (such as characters A through Z) and special characters (metacharacters), which describe one or more strings to match when looking for a text body. A regular expression, as a template, matches a character pattern to the string you are searching for.

In this article, we only introduce a simple regular primer foundation, and you can find out what you want to know about yourself!

The regular is introduced into Java after JDK1.4, the earliest JDK, if you want to use the regular, you need to add a regular development package Apache provided.

Pattern,matcher class

to apply a regular expression to a program, you must rely on the pattern class and the Matcher class, which are defined in the Java.util.regex package. Two mines. The main function of pattern class is to write the canonical specification, matcher the Chinese Traditional medicine, and verifies whether a string conforms to its specification .

Here are a few small procedures to understand whether the regular check is a number of words?
 Packagecom. regular expressions;ImportJava.util.regex.Pattern; Public classregexdemo02{ Public Static voidMain (String args[]) {string str= "1234567890";//This string is made up of numbers        if(Pattern.compile ("[0-9]+"). Matcher (str). Matches ()) {//using the regularSystem.out.println ("is made up of numbers! ") ; }Else{System.out.println ("Not made up of numbers!" ") ; }    }};

Print result: is made up of numbers!

Date check?
 Packagecom. regular expressions;ImportJava.util.regex.Pattern;ImportJava.util.regex.Matcher; Public classregexdemo03{ Public Static voidMain (String args[]) {string str= "1983-07-27";//string specifying the good one date formatString Pat = "\\d{4}-\\d{2}-\\d{2}";//specifying a good regular expressionPattern p = pattern.compile (PAT);//instantiating the pattern classMatcher m = p.matcher (str);//instantiate the Matcher class        if(M.matches ()) {//match for validation, use regularSYSTEM.OUT.PRINTLN ("date format is legal! ") ; }Else{System.out.println ("Date format is not legal!" ") ; }    }};

Print Result: Date format legal!

Splitting a string
 Packagecom. regular expressions;ImportJava.util.regex.Pattern;ImportJava.util.regex.Matcher; Public classregexdemo04{ Public Static voidMain (String args[]) {//requires that the characters inside be taken out, that is, by the number splitString str = "a1b22c333d4444e55555f";//specifying the good one stringString Pat = "\\d+";//specifying a good regular expressionPattern p = pattern.compile (PAT);//instantiating the pattern classString s[] = p.split (str);//perform a split operation         for(intx=0;x<s.length;x++) {System.out.print (s[x]+ "\ T") ; }    }};

Print results

Replace string
 Packagecom. regular expressions;ImportJava.util.regex.Pattern;ImportJava.util.regex.Matcher; Public classregexdemo05{ Public Static voidMain (String args[]) {//requires that the characters inside be taken out, that is, by the number splitString str = "a1b22c333d4444e55555f";//specifying the good one stringString Pat = "\\d+";//specifying a good regular expressionPattern p = pattern.compile (PAT);//instantiating the pattern classMatcher m = p.matcher (str);//instantiating an object of the Matcher classString newstring = M.replaceall ("_") ;    System.out.println (newstring); }};

Print results

Using regular expressions, you can easily complete the validation of strings, split, replace, and so on, for most grateful characters to escape, in real development rarely use the Matcher class and the pattern class, and directly use the string class provided in the regular support. Regular rules
character Description
\ Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape. For example, " n " matches the character " n ". " \n " matches a line break. Serial "matches" "and" "matches" " \\ \ \( ( .
^ Matches the starting position of the input string. If the multiline property of the RegExp object is set, ^ also matches the \n position after "or" \r .
$ Matches the end position of the input string. If the multiline property of the RegExp object is set, $ also matches the \n position before "" or "" \r .
* Matches the preceding subexpression 0 or more times. For example, zo* can match "" z and " zoo ". * Equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, " zo+ can match" zo and " zoo , but cannot match" " z . + equivalent to {1,}.
? Matches the preceding subexpression 0 or one time. For example, "" can Match "" or "" do(es)? does in "." does is do equivalent to {0,1}.
N N is a non-negative integer. Matches the determined n times. For example, "" o{2} cannot Match "" Bob in " o , but can match food two o in" ".
{N,} N is a non-negative integer. Match at least n times. For example, "" o{2,} cannot Match " Bob in" o , but can match foooood all o in "". "" is o{1,} equivalent to " o+ ". "" o{0,} is equivalent to " o* ".
{N,m} Both M and n are non-negative integers, where n<=m. Matches at least n times and matches up to M times. For example, "" o{1,3} will match the fooooood top three o in "". "" is o{0,1} equivalent to " o? ". Note that there can be no spaces between a comma and two numbers.
? When the character immediately follows any other restriction (*,+,?,{n},{n,},{n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string " oooo ", "" o+? will match a single "" and "" o o+ will match all " o ".
. Matches \ n any single character except "". To match \ n any character including "", use (.|\n) a pattern like "".
(pattern) Match pattern and get this match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use " \( or" \) .
(?:p Attern) Matches pattern but does not get a matching result, which means that this is a non-fetch match and is not stored for later use. This is useful in using the or character " (|) " to combine the various parts of a pattern. For example, " industr(?:y|ies) " is a industry|industries more abbreviated expression than "".
(? =pattern) Positive pre-check to match the find string at the beginning of any string matching pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, " Windows(?=95|98|NT|2000) can match" Windows2000 in Windows , but not " Windows3.1 in" Windows . Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
(?! Pattern Forward negation, matching the lookup string at the beginning of any mismatched pattern string. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example Windows(?!95|98|NT|2000) , "can match" Windows3.1 in Windows , but cannot match "" Windows2000 in Windows "". Pre-check does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check
(? <=pattern) Reverse affirmation pre-check, and positive forward certainly pre-check class quasi, just the opposite direction. For example, " (?<=95|98|NT|2000)Windows can match" 2000Windows in Windows , but not " 3.1Windows in" Windows .
(? <!pattern) Reverse negation of pre-check, and positive negative pre-check class quasi-, just the opposite direction. For example (?<!95|98|NT|2000)Windows , "can match" 3.1Windows in Windows , but cannot match "" 2000Windows in Windows "".
X|y Match x or Y. For example, " z|food can match" z or " food ." "" matches "" or "" (z|f)ood zood food .
[XYZ] The character set is combined. Matches any one of the characters contained. For example, "" can Match "" in "" [abc] plain a .
[^XYZ] Negative character set. Matches any character that is not contained. For example, "" can Match "" in "" [^abc] plain p .
[A-z] The character range. Matches any character within the specified range. For example, " [a-z] " can match a z any lowercase alphabetic character in the "to" range.
[^a-z] A negative character range. Matches any character that is not in the specified range. For example, " [^a-z] " can match any character that is not in the " a to" z range.
\b Matches a word boundary, which is the position between a word and a space. For example, "" can Match "in" er\b never er , but not " verb in" er .
\b Matches a non-word boundary. "" Can Match "in" er\B verb er , but not " never in" er .
\cx Matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is considered to be a literal " c " character.
\d Matches a numeric character. equivalent to [0-9].
\d Matches a non-numeric character. equivalent to [^0-9].
\f Matches a page break. Equivalent to \x0c and \CL.
\ n Matches a line break. Equivalent to \x0a and \CJ.
\ r Matches a carriage return character. Equivalent to \x0d and \cm.
\s Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s Matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].
\ t Matches a tab character. Equivalent to \x09 and \ci.
\v Matches a vertical tab. Equivalent to \x0b and \ck.
\w Matches any word character that includes an underscore. Equivalent to " [A-Za-z0-9_] ".
\w Matches any non-word character. Equivalent to " [^A-Za-z0-9_] ".
\xn Match N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, " \x41 match" A . "" \x041 is equivalent to " \x04&1 ". ASCII encoding can be used in regular expressions:
\num Matches num, where num is a positive integer. A reference to the obtained match. For example, " (.)\1 " matches two consecutive identical characters.
\ n Identifies an octal escape value or a backward reference. n is a backward reference if \ n is preceded by at least one of the sub-expressions obtained. Otherwise, if n is the octal number (0-7), N is an octal escape value.
\nm Identifies an octal escape value or a backward reference. If at least NM has obtained a subexpression before \nm, then NM is a backward reference. If there are at least N fetches before \nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.
\nml If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value NML is matched.
\un Match N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (©).
Common Regular Expressions
User name /^[a-z0-9_-]{3,16}$/
Password /^[a-z0-9_-]{6,18}$/
Password 2 (?=^. {8,}$) (? =.*\d) (? =.*\w+) (? =.*[a-z]) (? =.*[a-z]) (?!. *\n). *$ (consisting of numbers/uppercase/lowercase letters/punctuation, four kinds must have, 8 more)
Hexadecimal value /^#? ([a-f0-9]{6}| [A-f0-9] {3}) $/
E-Mail /^ ([a-z0-9_\.-]+) @ ([\da-z\.-]+) \. ([A-z\.] {2,6}) $/
/^[a-z\d]+ (\.[ a-z\d]+) *@ ([\da-z] (-[\da-z])?) +(\. {A} [a-z]+) +$/or \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Url /^ (https?:\ /\/)? ([\da-z\.-]+) \. ([A-z\.] {2,6}) ([\/\w \.-]*) *\/?$/or [a-za-z]+://[^\s]*
IP Address /((2[0-4]\d|25[0-5]| [01]?\d\d?] \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?] /
/^(?:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) \.) {3} (?: 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?) $/or ((2[0-4]\d|25[0-5]|[ 01]?\d\d?) \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?]
HTML tags /^< ([a-z]+) ([^<]+) * (?:> (. *) <\/\1>|\s+\/>) $/or < (. *) (. *) >.*<\/\1>|< (. *) \ >
Delete code \ \ Comment (? <!http:|\s)//.*$
Match double-byte characters (including kanji) [^\x00-\xff]
Kanji (character) [\u4e00-\u9fa5]
The range of Chinese characters in Unicode encoding /^[\u2e80-\u9fff]+$/
Chinese and full-width punctuation (characters) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
Date (year-month-day) (\d{4}|\d{2})-((0? ( [1-9])) | (1[1|2])) -((0? [1-9]) | ([12] ([1-9])) | (3[0|1]))
Date (month/day/year) ((0? [1-9] {1}) | (1[1|2])) /(0?) [1-9]| ([12][1-9]) | (3[0|1])) /(\d{4}|\d{2})
Time (Hours: minutes, 24-hour system) (1|0?) [0-9]|2[0-3]):([0-5][0-9])
Fixed telephone number in mainland China (\d{4}-|\d{3}-)? (\d{8}|\d{7})
Mobile phone number in mainland China 1\D{10}
China Mainland Postcode [1-9]\d{5}
China Mainland ID Number (15-bit or 18-bit) \D{15} (\d\d[0-9xx])?
Non-negative integers (positive integers or 0) \d+
Positive integers [0-9]*[1-9][0-9]*
Negative integer -[0-9]*[1-9][0-9]*
Integer -?\d+
Decimal (-?\d+) (\.\d+)?
Blank Line \n\s*\r or \ n \ nyou (editplus) or ^[\s\s]*\n
QQ number [1-9]\d{4,}

Basic Java---Regular expressions

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.