Home  >  Article  >  Backend Development  >  PHP regular expression collection

PHP regular expression collection

小云云
小云云Original
2017-11-15 16:11:373672browse

Regular expression, also known as regular expression, is called Regular Expression in English. It is often abbreviated as regex, regexp or RE in the code. It is a concept in computer science. Regular tables are usually used to retrieve and replace text that matches a certain pattern (rule). A regular expression is a logical formula that operates on strings (including ordinary characters (for example, letters between a to z) and special characters (called "metacharacters")), which uses some predefined specific characters. , and the combination of these specific characters form a "rule string". This "rule string" is used to express a filtering logic for strings. A regular expression is a text pattern that describes one or more strings to match when searching for text.

The following is a collection of some commonly used regular expressions:

Only numbers can be entered: "^[0-9]*$"

Only n digits can be entered Numbers: "^\d{n}$"

Only numbers with at least n digits can be entered: "^\d{n,}$"

Only m ~ n digits can be entered Numbers: "^\d{m,n}$"

Only numbers starting with zero and non-zero can be entered: "^(0|[1-9][0-9]*)$"

You can only enter positive real numbers with two decimal places: "^[0-9]+(.[0-9]{2})?$"

You can only enter 1 ~Positive real numbers with 3 decimal places: "^[0-9]+(.[0-9]{1,3})?$"

Only non-zero positive integers can be entered: "^\ +?[1-9][0-9]*$"

Only non-zero negative integers can be entered: "^\-[1-9][0-9]*$"

Only characters with a length of 3 digits can be entered: "^.{3}$"

Only a string of 26 English letters can be entered: "^[A-Za-z]+$ "

Only a string consisting of 26 uppercase English letters can be entered: "^[A-Z]+$"

Only a string consisting of 26 lowercase English letters can be entered:" ^[a-z]+$"

You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$"

Only Enter a string consisting of numbers, 26 English letters or underscores: "^\w+$"

Verify user password: "^[a-zA-Z]\w{5,17}$" Correct Format bit: starts with a letter, has a length between 6 and 18, and can only contain characters, numbers and underscores

Verify whether it contains illegal characters ^(?:[\u4e00-\u9fa5]*\w*\ s*)+$

Only Chinese characters can be entered: "^[\u4e00-\u9fa5]{0,}$"

Verify email address: "^\w+([-+. ]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"

Verify InternetURL:"^http://([\ w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"

China Phone Number Verification

Matching Form Such as: 0511-4405222 or 021-87888822 or 021-44055520-555 or (0511)4405222

Regular expression "((d{3,4})|d{3,4}-)?d {7,8}(-d{3})*"

China Postal Code Verification

Matching format such as: 215421

Regular expression "d{6}"

Email verification

The matching form is: justali@justdn.com

Regular expression "w+([-+.]w+)*@w+([-. ]w+)*.w+([-.]w+)*"

ID card verification

Matching format such as: 15-digit or 18-digit ID card

Regular expression "d{18}|d{15}"

Commonly used number verification

Regular expression

"d{n}" n is the specified length

"d{n,m}" The length range from n to m

Illegal character verification

Match characters that exclude illegal characters such as:

Regular expression^(? :[\u4e00-\u9fa5]*\w*\s*)+$

Date verification

The matching format is: 20030718,030718

Range: 1900-- 2099

Regular expression ((((19){1}|(20){1})d{2})|d{2})[01]{1}d{1}[0 -3]{1}d{1}

<html>
<head>
<title>php正则表达式集锦实例</title>
<script language="javascript" type="text/javascript">
function Button1_onclick(){
if(!test_hanzi(document.form1.Text1.value)){
alert("姓名只能是汉字");
return false;
}
return true;
}
//验证电子邮件
function test_email(strEmail){ 
var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
if(myReg.test(strEmail)) return true;
return false;
} 
//只能输入数字
function test_shuzi(strshuzi){ 
var myReg =/^[0-9]*$/;
if(myReg.test(strshuzi)) return true;
return false;
} 
//只能输入汉字
function test_hanzi(strhanzi){ 
var myReg =/^[\u4e00-\u9fa5]{0,}$/;
if(myReg.test(strhanzi)) return true;
return false;
}
</script>
</head>
<body>
<form name="form1">姓名:
    <input id="Text1" name="Text1" type="text" />
    <input name="按钮" type="button" id="Button1" onclick="return Button1_onclick()" value="button" language="javascript" />
</form>
</body>
</html>

The above is a collection of PHP expressions. I hope it is helpful to everyone and can be used in future work.

Related recommendations:

Detailed explanation of commonly used functions in php regular expressions

Instance analysis of php regular expressions

The ten most practical PHP regular expressions

Summary of regular expressions commonly used in PHP forms

Summary of commonly used regular expressions

The above is the detailed content of PHP regular expression collection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn