Common Regular Expressions (php asp JavaScript) Learning Manual

Source: Internet
Author: User
Tags alphabetic character control characters html tags numeric lowercase numeric value pow trim

Some common regular expressions

"^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+)? $"//nonnegative 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 of 26 English letters

"^[a-z]+$"//A string of 26 uppercase letters

"^[a-z]+$"///a string consisting of 26 lowercase letters

"^[a-za-z0-9]+$"//A string of numbers and 26 English letters

"^w+$"//A string of numbers, 26 English 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

"(d+-)?" (d{4}-?d{7}|d{3}-?d{8}|^d{7,8}) (-d+)? " 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

Matching regular expressions for Chinese characters: [U4E00-U9FA5]

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

A regular expression that matches a blank row: n[s|] *r

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

Matching a regular expression with a trailing space: (^s*) | (s*$)

Regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *

A regular expression that matches URL URLs: ^[a-za-z]+://(\w+ (-\w+) *) (\. \w+ (-\w+) *)) * (\?\s*)? $

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$

Match domestic phone number: (d{3}-|d{4}-)? (D{8}|d{7})?

Matching Tencent QQ Number: ^[1-9]*[1-9][0-9]*$

Application in JavaScript

Calculates the length of a string (a double-byte character length meter 2,ascii character 1)

The code is as follows Copy Code

String.prototype.len=function () {return This.replace ([^x00-xff]/g, "AA"). Length;}

A regular expression that matches a blank row: n[s|] *r

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

Matching a regular expression with a trailing space: (^s*) | (s*$)

Application: JavaScript does not have a trim function like VBScript, we can use this expression to implement, as follows:

The code is as follows Copy Code

String.prototype.trim = function ()
{
Return This.replace (/(^s*) | ( s*$)/g, "");
}

To decompose and transform an IP address using a regular expression:

The following is a JavaScript program that uses a regular expression to match an IP address and converts an IP address to a corresponding numeric value:

The code is as follows Copy Code

function IP2V (IP)
{
re=/(d+). (d+). (d+). (d+)/g//matching the regular expression of the IP address
if (Re.test (IP))
{
Return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1
}
Else
{
throw new Error ("not a valid IP address!")
}
}

However, if the above program does not use a regular expression, and the split function directly to decompose may be simpler, the program is as follows:

The code is as follows Copy Code

var ip= "10.100.20.168″
Ip=ip.split (".")
Alert ("IP value is:" + (IP[0]*255*255*255+IP[1]*255*255+IP[2]*255+IP[3]*1))

Regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *

A regular expression that matches URL URLs: http://([w-]+.) +[w-]+ (/[w-/?%&=]*)?

Using regular expressions to remove the repeated characters in the string algorithm program: [Note: This program is incorrect, the reason see this post reply]

The code is as follows Copy Code

var s= "Abacabefgeeii"
var s1=s.replace (/(.). *1/g, "$1″)
var re=new RegExp ("[" +s1+ "]", "G")
var s2=s.replace (Re, "")
Alert (S1+S2)//Result: ABCEFGI

I used to post on the csdn to find an expression to achieve the elimination of repeated characters, and ultimately did not find, this is the simplest way I can think of implementation. The idea is to use a back reference to take out the characters that contain duplicates, and then to create a second expression with a repeating character, with a concatenation of the characters that are not repeated. This method may not apply to strings that are required for character order.

You have to use regular expressions to extract the filename from the URL address of the JavaScript program, the following result is Page1

The code is as follows Copy Code

S= "http://www.111cn.net/nokia/5230/"
S=s.replace (/. * *) {0,} ([^.] +). */ig, "$2″"
Alert (s)


PHP regularization functions and usage

Preg_match () and Preg_match_all ()
Preg_quote ()
Preg_split ()
Preg_grep ()
Preg_replace ()
The specific use of the function, we can find through the PHP manual, the following share some of the regular expressions accumulated:

Match Action Property

The following are the referenced contents:

The code is as follows Copy Code
$str = ';
$match = ';
Preg_match_all ('/s+action= ') (?!) http:) (. *?) " S/', $str, $match);
Print_r ($match);

Using callback functions in regular

The code is as follows Copy Code

/**
* Replace some string by callback function
*
*/
function Callback_replace () {
$url = ' http://esfang.house.sina.com.cn ';
$str = ';
$str = Preg_replace ('/<=saction= ') (?!) http:) (. *?) (? = "s)/e ', ' Search ($url, \1) ', $str);

Echo $str;
}

function Search ($url, $match) {
Return $url. '/' . $match;
}

Some regular basic syntax

The

X|y matches x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches ' zood ' or ' food '. The
[XYZ] Character set is combined. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '. The
[^xyz] Negative character set is combined. Matches any characters that are not included. For example, ' [^ABC] ' can match ' P ' in ' plain '. The
[A-z] character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the range ' a ' to ' Z '. The
[^a-z] 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 range of ' a ' to ' Z '.
B matches a word boundary, which refers to the position between the word and the space. For example, ' Erb ' can match ' er ' in ' never ', but cannot match ' er ' in ' verb '.
B matches a non-word boundary. ' ErB ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '. The
CX matches the control characters indicated by X. For example, CM matches a control-m or carriage return character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as 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 feed character. Equivalent to x0c and CL.
N matches a newline character. Equivalent to x0a and CJ.
R matches a return character. Equivalent to x0d and CM.
S matches any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].
S matches any non-white-space character. equivalent to [^ FNRTV]. The
T matches a tab character. Equivalent to x09 and CI. The
V matches a vertical tab. Equivalent to x0b and CK. The
W matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '. The
W matches any non-word character. Equivalent to ' [^a-za-z0-9_] '.

Related Article

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.