Test 10 regular expressions (Java)

Source: Internet
Author: User
Tags uppercase character tld

Regular Expression

1. username regular expression mode (test code please download) ^ [a-z0-9 _-] {3, 15} $ ^ # line start [a-z0-9 _-] # match characters in the list, a-Z, 0-9, underline, with a length of at least 3 characters and a maximum length of 15 $ # End of line 2. password regular expression mode ((? =. * \ D )(? =. * [A-Z]) (? =. * [A-Z]) (? =. * [@ # $ %]). {6, 20}) (# Start group (? =. * \ D) # must contain a number 0-9 (? =. * [A-Z]) # It must contain a lowercase character (? =. * [A-Z]) # must contain an uppercase character (? =. * [@ # $ %]) # It must contain a special character "@ # $ %" in the list ". # Check whether all strings match the preceding conditions {6, 20} # the string length must be at least 6 Characters and the maximum length is 20) # End of group 3. hexadecimal color code Regular Expression Pattern ^ # ([A-Fa-f0-9] {6} | [A-Fa-f0-9] {3 }) $ ^ # line start # must contain a "#" symbol (# group #1 start [A-Fa-f0-9] {6} # any string in the list, with a length of 6 | #.. or any string in the [A-Fa-f0-9] {3} # list with a length of 3) # group #1 end $ # Row end 4. email Regular Expression Pattern ^ [_ A-Za-z0-9-] + (\\. [_ A-Za-z0-9-] +) * @ [A-Za-z0-9] + (\\. [A-Za-z0-9] + )*(\\. [A-Za-Z] {2,}) $ ^ # Start of the row [_ A-Za-z0-9-] + # It must start with a character in brackets Character [], must contain one or more (+) (# group #1 start \\. [_ A-Za-z0-9-] + # Next is a point ". "And the character [] in the brackets must contain one or more (+) * # The end of group #1. This group is optional (*) @ # must contain a "@" symbol [A-Za-z0-9] + # Next is the character in brackets [], must contain one or more (+) (# group #2 start-level TLD check \\. [A-Za-z0-9] + # Next is a point ". "And the character [] in the brackets must contain one or more (+) * # group #2 ends. This group is optional (*) (# group #3 Start-level TLD check \\. [A-Za-Z] {2,} # Next is a point ". "and [] characters in the brackets. The minimum length is 2) # group #3 end $ # End Row 5. image File Extension regular expression mode ([^ \ s] + (\\. (? I) (JPG | PNG | GIF | BMP) $) (# group #1 start [^ \ s] + # must contain one or more arbitrary characters (except spaces) (# group #2 start \\. # Next is a point ". "(? I) # ignore the case sensitivity check of the following characters (# group #3 Start JPG # contain the character "jpg" | #.. or PNG # contains the character "PNG" | #.. or GIF # contains the character "GIF" | #.. or BMP # contains the character "BMP") # group #3 end) # group #2 end $ # string end) # group #1 end to add one: [\ s] *? ([^ \/] *? \. Jpg) 6. IP address Regular Expression Mode ^ ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) $ ^ # Start row (# group #1 start [01]? \ D? # It can be 1 to 2 digits. if three numbers appear, they must start with 0 or 1 # For example ([0-9], [0-9] [0-9], [0-1] [0-9] [0-9]) | #... or 2 [0-4] \ D # starts with 2, followed by 0-4, and ends with any number (2 [0-4] [0-9]) | #... or 25 [0-5] # Start with 2, followed by 5, and end with 0-5 (25 [0-5]) # End with group #1 \\. # Next, click ". ".... # Repeat 3 times (3x) $ # End of line 7. time Format regular expression mode 12-hour time regular expression mode (1 [012] | [1-9]): [0-5] [0-9] (\ s )? (? I) (am | PM) (# group #1 start 1 [012] # Start with 10, 11, 12 | # Or [1-9] # Start with 1, 2 ,... 9) # group #1 end: # Next is a colon (:) [0-5] [0-9] # Next is 0 .. 5 and 0 .. 9, meaning 00 to 59 (\ s )? # Next is a space (optional )(? I) # The following checks are case insensitive (am | pm) # Next is the AM or pm24 hour-based time regular expression mode ([01]? [0-9] | 2 [0-3]): [0-5] [0-9] (# group #1 start [01]? [0-9] # Start with 0-9, 1-9, 00-09,10-19 | # Or 2 [0-3] # Start with 20-23) # End with group #1: # Next is a colon (:) [0-5] [0-9] # Next is 0 .. 5 and 0 .. 9, meaning 00 to 598. date Format (dd/mm/yyyy) regular expression mode (0? [1-9] | [12] [0-9] | 3 [01])/(0? [1-9] | 1 [012])/(19 | 20) \ D) (# group #1 start 0? [1-9] #01-09 or 1-9 | #.. or [12] [0-9] #10-19 or 20-29 | #.. or 3 [01] #30, 31) # group #1 end/# Next is a "/" (# group #2 start 0? [1-9] #01-09 or 1-9 | #.. or 1 [012] #10, 11, 12) # group #2 end/# Next is a "/" (# group #3 Start (19 | 20) \ D #19 [0-9] [0-9] Or 20 [0-9] [0-9]) # group #3 end 9. regular Expression mode of HTML tags <(\ "[^ \"] * \ "| '[^'] * '| [^' \">]) *> <# Start with "<" label (# group #1 start \ "[^ \"] * \ "# Only two double quotation marks can be used in pairs-" string "| #.. or '[^'] * '# Only two single quotes can appear in pairs-'string' | #.. or [^ '\ ">] # Separate double quotation marks, single quotation marks, and"> "cannot appear ") # group #1 end * #0 or multiple times> # end label ">" end 10. HTML link regular expression mode: a tag regular expression mode in HTML (? I) <A ([^>] +)> (. + ?) </A> (# group #1 start? I # all checks are case sensitive) # group #1 end <A # Start with "<a" (# group #2 start with [^>] + # Except ("> ") any character except, at least one character) # group #2 end> # Next is "> "(. + ?) # Match all </a> # extract HTML link Regular Expression Pattern \ s *(? I) href \ s * = \ s * (\ "([^ \"] *) \ "| '[^'] * '| ([^' \"> \ s] +); \ s * # It can start with a space (? I) # all checks are case-insensitive href # Next is an "href" character \ s * = \ s * # spaces are allowed on both sides of the equal sign, (# group #1 start "([^"] * ") # Only two double quotation marks are allowed to appear in pairs-" string "| #.. or '[^'] * '# Only two single quotes are allowed in pairs-'string' | #.. or ([^ '">] +) # or separate single quotes, double quotes, and"> "are not allowed) # group #1 end D: \ Java> JAVA testten Test 1: username regular expression "j123 _" exact match "^ [a-z0-9 _-] {3,15} $ "? True Test 2: The regular expression "j123_j @" exact match "((? =. * \ D )(? =. * [A-Z]) (? =. * [A-Z]) (? =. * [@ # $ %]). {6, 20 })"? True test 3: hexadecimal color code "# af0067" exact match "^ # ([A-Fa-f0-9] {6} | [A-Fa-f0-9] {3}) $ "? True Test 4: E-mail regular expression "j1242@sina.com" exact match "^ ([_ A-Za-z0-9-] +) @ [A-Za-z0-9] + (\. [A-Za-z0-9] + )*(\. [A-Za-Z] {2,}) $ "? True Test 5: Image File Extension "j123 _. jpg" exact match "([^ \ s] + (\.(? I) (JPG | PNG | GIF | BMP) $ )"? True test 6: IP address regular expression "192.168.1.11" exact match "^ ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) \. ([01]? \ D? | 2 [0-4] \ d | 25 [0-5]) $ "? True Test 7: Regular Expression of time format "PM" exact match "(1 [012] | [1-9]): [0-5] [0-9] (\ s )? (? I) (am | pm )"? True test 8: Date Format (dd/mm/yyyy) "02/09/2012" exact match "(0? [1-9] | [12] [0-9] | 3 [01])/(0? [1-9] | 1 [012])/(19 | 20) \ D )"? True test 9: HTML Tag regular expression " "exact match" <("[^"] * "|' [^ '] *' | [^ '">]) *> "? True test 10: Regular Expression of HTML link "<a href = 'www .baidu.com '> www.baidu.com </a>" exact match ".*? (? I) href \ s * = \ s * ("([^"] *) "| '[^'] * '| [^'"> \ s] + ). *? "? True

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.