The following format requirements apply to domain names:
1. The labels in a domain name are composed of English letters and numbers. Each label cannot exceed 63 characters and is not case sensitive. You cannot use other punctuation marks except hyphens.
2. Domain names at all levels are connected by real-time (.). The length of a third-level domain name cannot exceed 20 characters.
3. A complete domain name composed of multiple labels cannot exceed 255 characters.
Therefore, the regular expression that matches the domain name must be
1, composed of English numbers and "_" [-a-z0-9]
2. Connect each level "."
3. Level 3 domain names cannot exceed 20}
The test format is as follows:
The code is as follows: |
Copy code |
<? Php $ Url = 'www .111cn.net '; $ Search = '/--- regular N ---/'; If (preg_match ($ search, $ url )){ Echo 'matched '; } Else { Echo 'mismatched '; } ?>
|
I will summarize several common online
Regular 1
B ([a-z0-9] + (-[a-z0-9] +) *.) + [a-z] {2,} B
Error match: length> 60
Regular 2
The code is as follows: |
Copy code |
^ ([^-] [A-z0-9A-Z-_] + .) *) [^-] [a-z0-9A-Z-_] + (. [a-zA-Z] {2, 4}) {1, 2} $ |
Error match: length> 60
Error match: www. te_st.com
Regular 3
The code is as follows: |
Copy code |
([Wd-_] + .):? [^-_]) + W {2, 4} |
Mismatch: www.111cn.net.cn
Error match: www. te_st.com
Regular 4
The code is as follows: |
Copy code |
[A-zA-Z0-9] [-a-zA-Z0-9] {} (. [a-zA-Z0-9] [-a-zA-Z0-9] {}) + .? |
No errors
Regular 5
The code is as follows: |
Copy code |
(? <=.) ([A-zA-Z0-9] ([a-zA-Z0-9-] {} [a-zA-Z0-9])?.) + [A-zA-Z] {2, 6} |
Second-level domain name not matched
Regular 6
The code is as follows: |
Copy code |
([A-z0-9] [a-z0-9-] *?. (? : Com | cn | net | org | gov | info | la | cc | co )(? :.(? : Cn | jp ))?) $ |
Many top-level domain names do not match
Example
The code is as follows: |
Copy code |
<? Php $ Search = '~ ^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (? ([^ #] *)? (#(.*))?~ I '; $ Url = 'http: // www.111cn.net/pub/ietf/uri/#gonn '; $ Url = trim ($ url ); Preg_match_all ($ search, $ url, $ rr ); Printf ("<p> output URL data: </p> <pre> % s </pre> n", var_export ($ rr, TRUE )); /* The groups are as follows: $1 = http: $2 = http $3 = // www.111cn.net $4 = www.111cn.net $5 =/pub/ietf/uri/ $6 = <undefined> $7 = <undefined> $8 = # Gonn $9 = Gonn */ ?> |