Collection of common Regular Expressions and Regular Expressions

Source: Internet
Author: User
Tags php regular expression preg

Collection of common Regular Expressions and Regular Expressions

In computer science, regular expressions are used to describe or match a single string that complies with a certain syntactic rule. In WEB development, regular expressions are usually used to detect and search for strings that meet the rules, such as checking whether the user input E-mai format is correct and collecting page content that meets the rules.

Today, I will use PHP and Javascript to introduce the most commonly used and useful regular expressions in WEB development and their usage. Regular Expressions are a subject and cannot be explained in an article, there are many theoretical things on the Internet. If you are interested, you can search for them. However, you may not have to study the incomprehensible regular expressions. Read this article and examples to show you common and practical regular expressions.

Common PHP expressions

1. match a positive integer:/^ [1-9] \ d * $/

2. match a non-negative integer (positive integer + 0):/^ \ d + $/

3. Match Chinese characters:/^ [\ x {4e00}-\ x {9fa5}] + $/u

4. matching Email:/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/

5. Match URL :( (f | ht) {1} (tp | tps): //) [-a-zA-Z0-9 @: % _ \ + .~ #? & // =] +)

6. Match the start of a letter, 5-16 characters, letter and digit underline:/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/

7. Match numbers, letters, underscores, Chinese:/^ [\ x {4e00}-\ x {9fa5} A-Za-z0-9 _] + $/u

8. Match the Chinese zip code:/^ [1-9] \ d {5} $/

9. Matched IP Address:/\ B (? :(? : 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]?) \ B/

10. match Chinese mainland ID card:/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} (\ d | x | X) $/

PHP Regular Expression verification string method example:

$ Str = "Chinese"; $ preg = "/^ [\ x {4e00}-\ x {9fa5}] + $/u "; // match Chinese if (preg_match ($ preg, $ str, $ arr) {$ msg = 'match successful! ';} Else {$ msg =' match failed! ';} Echo $ msg;


Common Javascript expressions

1. match a positive integer:/^ [0-9] * [1-9] [0-9] * $/

2. match a non-negative integer (positive integer + 0):/^ \ d + $/

3. Match Chinese characters:/^ [\ u4e00-\ u9fa5]/

4. matching Email:/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/

5. URL matching URL:/^ (f | ht) {1} (tp | tps): \/([\ w-] + \.) + [\ w-] + (\/[\ w -. /? % & =] *)? /

6. Match the start of a letter, 5-16 characters, letter and digit underline:/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/

7. Match numbers, letters, underscores, Chinese:/^ [\ u4e00-\ u9fa5A-Za-z0-9 _] + $/

8. Match the Chinese zip code:/^ [1-9] \ d {5} $/

9. Matched IP Address:/\ B (? :(? : 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]?) \ B/

10. match Chinese mainland ID card:/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} (\ d | x | X) $/

Example of the string method for Javascript Regular Expression Verification:

Var str = "abc@126.com"; var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; // match Email if (preg. test (str) {var msg = "matched successfully";} else {var msg = "matched failed! ";} Alert (msg );


 
Well, today's regular expression will talk about this. I hope you will like my tutorial and get help from it. We will try again next time.


Some common Regular Expressions

Common Regular Expressions
Code role Regex r = new Regex (".*? [^ \ '\ "] +) [\' | \"]. *?>. *? "); Find whether the image Regex rt = new Regex (@" script | iframe | \ '|; | ", RegexOptions. ignoreCase); find whether the removal script, frame, single quotes are usually used to verify URL transfer parameters, anti-SQL: Regex r = new Regex ("</font> | <font [^ <>] +> | font-size :. *?
Pt | font-size :.*? Px | <span [^ <>] +> | </span> |
<Span [^ <>] +> | </p> | <p [^ <>] +> | ", RegexOptions. IgnoreCase );
ListBox_37.DataTable.Rows [I] ["title"] = r. replace (ListBox_37.DataTable.Rows [I] ["title"]. toString (). replace ("<br/> ",""). toLower (), ""); find whether the font style label is included
Used to remove the font style Regex rTel = new Regex ("^ [0-9-] {8 }$ | ^ [0-9-] {11, 12} $ ");
Boolean ismatch = rTel. IsMatch (p_phone.Value); Verify the input
Used to verify the telephone number RegexrEmail = newRegex ("^ \ w + (-\ w +) | (\\. \ w +) * \ @ [A-Za-z0-9] +
(\. |-) [A-Za-z0-9] +) * \. [A-Za-z0-9] + $ "); Verify the input
Used to verify the mailbox format private string RemoveHtml (string strHtml)
{
String strhtml = Regex. Replace (strHtml, "<. ++?> ","");
Strhtml = Regex. Replace (strhtml, "<br/>", "", RegexOptions. IgnoreCase );
Return strhtml;
} Clear all the tags in the input parameter and return the private string StripHT (string strHtml)
{
Regex regex = new Regex ("<. +?> ", RegexOptions. IgnoreCase );
String strOutp... the remaining full text>

Who has the regular expression usage? A document or something, which contains some common regular expressions!

Sent, *** hua@yeah.net. Please check!

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.