Collect and share common regular expressions in php

Source: Internet
Author: User

Get all image URLs preg_match_all ("/src = (" | ') {0,} (http: // (. + ?)) ("| '| S |>)/is", $ text, $ img );

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Match double byte characters (including Chinese characters): [^ x00-xff]
Regular expression for matching empty rows: n [s |] * r
Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
Regular expression matching the first and last Spaces: (^ s *) | (s * $)
Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Regular Expression Matching URL: ^ [a-zA-z] +: // (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $
Match Chinese phone number: (d {3}-| d {4 }-)? (D {8} | d {7 })?
Match Tencent QQ number: ^ [1-9] * [1-9] [0-9] * $ <span id = "more-58"> </span>
"^ D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? D + $ "// integer
"^ D + (. d + )? $ "// Non-negative floating point number (positive floating point number + 0)
"^ ([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// positive floating point number
"^ (-D + (. d + )?) | (0 + (. 0 + )?)) $ "// Non-positive floating point number (negative floating point number + 0)
"^ (-([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point number
"^ (-? D +) (. d + )? $ "// Floating point number
"^ [A-Za-z] + $" // A string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ W + $" // a string consisting of digits, 26 letters, or underscores
"^ [W-] + (. [w-] +) * @ [w-] + (. [w-] +) + $" // email address
"^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $ "// Url
/^ (D {2} | d {4})-(0 ([1-9] {1}) | (1 [1 | 2]) -([0-2] ([1-9] {1}) | (3 [0 | 1]) $ // year-month-day
/^ (0 ([1-9] {1}) | (1 [1 | 2]) /([0-2] ([1-9] {1}) | (3 [0 | 1]) /(d {2} | d {4}) $ // month/day/year
"^ ([W-.] +) @ ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }.) | ([w-] + .) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (]?) $ "// Emil
/^ (+? [0-9] {2, 4}-[0-9] {3, 4}-) | ([0-9] {3, 4 }-))? ([0-9] {7,8}) (-[0-9] + )? $ // Phone number
"^ (D {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]) $ "// IP address

Metacharacters and their behavior in the context of regular expressions:

Mark the next character as a special character, a literal character, a back reference, or an octal escape character.

^ Matches the start position of the input string. If the Multiline attribute of the RegExp object is set, ^ matches the position after 'N' or 'R.

$ Matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches the location before 'n' or 'R.

* Matches the previous subexpression zero or multiple times.

+ Match the previous subexpression once or multiple times. + Is equivalent to {1 ,}.

? Match the previous subexpression zero or one time .? It is equivalent to {0, 1 }.

{N} n is a non-negative integer that matches the specified n times.

{N ,}n is a non-negative integer and matches at least n times.

Both {n, m} m and n are non-negative integers, where n <= m. Match at least n times and at most m times. There must be no space between a comma and two numbers.

? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible.

. Match any single character except "n. To match any character including 'n', use a pattern like '[. n.
(Pattern) matches pattern and obtains this match.

(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use.

(? = Pattern) forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use.

(?! Pattern) negative pre-query, and (? = Pattern).

X | y matches x or y.

[Xyz] character set combination.

[^ Xyz] combination of negative character sets.

[A-z] character range, matching any character in the specified range.

[^ A-z] the negative character range matches any character that is not within the specified range.

B matches a word boundary, that is, the position between a word and a space.

B matches non-word boundaries.

Cx matches the control characters specified by x.

D matches a numeric character. It is equivalent to [0-9].

D. Match a non-numeric character. It is equivalent to [^ 0-9].

F matches a form feed. It is equivalent to x0c and cL.

N matches a linefeed. It is equivalent to x0a and cJ.

R matches a carriage return. It is equivalent to x0d and cM.

S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [fnrtv].

S matches any non-blank characters. It is equivalent to [^ fnrtv].

T matches a tab. It is equivalent to x09 and cI.

V matches a vertical tab. It is equivalent to x0b and cK.

W matches any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.

W matches any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.

Xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers.

Num matches num, where num is a positive integer. References to the obtained matching.

N identifies an octal escape value or a backward reference. If n contains at least n obtained subexpressions, n is a back reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

Nm identifies an octal escape value or a backward reference. If there are at least is preceded by at least nm obtained subexpressions before nm, then nm is backward reference. If at least n records are obtained before nm, n is a backward reference followed by m. If none of the preceding conditions are met, if n and m are octal numbers (0-7), nm matches the octal escape value nm.

If n is an octal digit (0-3) and m and l are octal numerals (0-7), nml matches the octal escape value nml.

Un matches n, where n is a Unicode character represented by four hexadecimal numbers.

Regular Expression Matching Chinese characters: [u4e00-u9fa5]

Match double byte characters (including Chinese characters): [^ x00-xff]

Regular expression for matching empty rows: n [s |] * r

Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/

Regular expression matching the first and last Spaces: (^ s *) | (s * $)

Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*

The regular expression matching the URL: http: // ([w-] +.) + [w-] + (/[w -./? % & =] *)?

Use regular expressions to restrict text box input in a webpage form:

You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ u4E00-u9FA5]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ u4E00-u9FA5]/g ,''))"

You can only enter the full-width characters: onkeyup = "value = value. replace (/[^ uFF00-uFFFF]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ uFF00-uFFFF]/g ,''))"

Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ d]/g ,''))"

You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ d]/g ,''))"

========= Regular expressions

Regular Expression Matching Chinese characters: [u4e00-u9fa5]

Match double byte characters (including Chinese characters): [^ x00-xff]

Regular expression for matching empty rows: n [s |] * r

Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/

Regular expression matching the first and last Spaces: (^ s *) | (s * $)

Regular Expression Matching IP address:/(d +). (d +)/g //

Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*

The regular expression matching the URL: http: // (/[w-] +.) + [w-] + (/[w -./? % & =] *)?

SQL statement: ^ (select | drop | delete | create | update | insert). * $

1. Non-negative integer: ^ d + $

2. Positive integer: ^ [0-9] * [1-9] [0-9] * $

3. Non-positive integer: ^ (-d +) | (0 +) $

4. Negative integer: ^-[0-9] * [1-9] [0-9] * $

5. Integer: ^ -? D + $

6. Non-negative floating point number: ^ d + (. d + )? $

7. Positive floating point: ^ (0-9) +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

8. Non-positive floating point number: ^ (-d +. d + )?) | (0 + (. 0 + )?)) $

9. Negative floating point number: ^ (-(regular expression of positive floating point number) $

10. English string: ^ [A-Za-z] + $

11. English capital string: ^ [A-Z] + $

12. Lowercase English string: ^ [a-z] + $

13. English character numeric string: ^ [A-Za-z0-9] + $

14. English digit and underline string: ^ w + $

15. Email Address: ^ [w-] + (. [w-] +) * @ [w-] + (. [w-] +) + $

16. URL: ^ [a-zA-Z] +: // (w + (-w + )*)(. (w + (-w + )*))*(? S *)? $
Or: ^ http: // [A-Za-z0-9] +. [A-Za-z0-9] + [/=? % -&_~ '@ []': +!] * ([^ <> ""]) * $

17. Zip code: ^ [1-9] d {5} $

18, Chinese: ^ [u0391-uFFE5] + $

19. Phone number: ^ (d {2, 3}) | (d {3 }-))? (0d {2, 3}) | 0d {2, 3 }-)? [1-9] d {6, 7} (-d {1, 4 })? $

20. Mobile phone number: ^ (d {2, 3}) | (d {3 }-))? 13d {9} $

21. Double byte characters (including Chinese characters): ^ x00-xff

22. Match the first and last Spaces: (^ s *) | (s * $) (trim functions like vbscript)

23. Matching HTML tags: <(. *)>. * </1> | <(. *)/>

24. Match null rows: n [s |] * r

25. Extract the network link in the Information: (h | H) (r | R) (e | E) (f | F) * = * ('| ")? (W | \ |/|.) + ('| "| * |> )?

26. Email address extracted from the information: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*

27. Extract the image link from the Information: (s | S) (r | R) (c | C) * = * ('| ")? (W | \ |/|.) + ('| "| * |> )?

28. Extract the IP address in The Information: (d +). (d +)

29. Extract the Chinese mobile phone number from The Information: (86) * 0 * 13d {9}

30. Extract the Chinese landline number from The Information: (d {3, 4}) | d {3, 4}-| s )? D {8}

31. Extract Chinese phone numbers (including mobile and landline phone numbers) from the Information: (d {3, 4}) | d {3, 4}-| s )? D {7, 14}

32. Chinese ZIP code extracted from the information: [1-9] {1} (d +) {5}

33. Extract floating point numbers (decimal places) in The Information ):(-? D *).? D +

34. Extract any number from The Information :(-? D *) (. d + )?

35. IP address: (d +). (d +)

36. Phone no.:/^ 0d {2, 3} $/

37. Tencent QQ No.: ^ [1-9] * [1-9] [0-9] * $

38. Account (starting with a letter, may be 5-16 bytes, can contain letters and numbers underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $

39. Chinese, English, numbers, and underscores: ^ [u4e00-u9fa5_a-zA-Z0-9] + $

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.