cool hit counter regular expression_Intefrankly

regular expression

Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission.


When we search for files in the local computer, Often, you will use“ * ” harmony“ ? ” to replace characters of any length and any single character; In the search engine with" | " come in place of perhaps the relationship between, double quote“” between indicates that the entire keyword must be included, etc.。

Look at another one.SQL statement:select * from T_Employee where eName like ' what?%', i.e. search all surnames“ what?” employees, The results of the implementation are as follows:

These all involve character matching.

regular expression what is it? ?

and the above“*”、“?” isotope similarity, regular expression It is also used to description String format that conforms to certain rules tools, despiteASP.NET、JQuery etc. provides validation controls, merely regular expression More complex and rigorous norms developed, Usually used to match、 Handling strings。

For example:

Specify the length of the registered user name as6 up to18 location, Can only be underlined by、 Number and letter composition, regular expression because of:/^[a-z0-9_-]{6,18}$/

Requirement that the real name be in Chinese, regular expression because of:/^[u4e00-u9fa5]+$/

Specify that the input must be in the correct mailbox format, with a regular expression of /^([a-z0-9_.-]+)@([da-z.-]+). ([a-z.]) {2,6}) $/


These pairs of strings of“ bind”, Both can be used regular expression come round description。

You're sure to be dazzled by the sight of, right regular expression turn off, Don't be afraid., Teachers always say, You can't remember., So we just need to be able to find it when we need to use it, I'm sure no one goes to that much trouble to memorize this stuff。 definitely, Learn about some of the more frequently occurring meta-character( regular expression The elements in the) It can help us to be faster“ read and understand” those regular expression。

common meta-character and what it means:



Mark the next character as a special character、 perhaps An original meaning character、 perhaps One backwards. quote、 perhaps An octal escape character。 for example,“n” matching character“n”。“ "Match a line break. The sequence "\" matches "" while "(" matches "(".


Matches the start position of the input string. If the Multiline property of the RegExp object is set, ^ also matches " ” perhaps“ ” Position after。


Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches " " or " "Previous position.


Match the preceding subexpression zero times perhaps several times。 for example,zo* compatible“z” and“zoo”。* tantamount to{0,}。


Match the preceding subexpression once perhaps several times。 for example,“zo+” compatible“zo” and“zoo”, But it doesn't match.“z”。+ tantamount to{1,}。


Match the preceding subexpression zero times perhaps once。 for example,“do(es)?” Can match“do” perhaps“does” hit the target“do”。? tantamount to{0,1}。


n is a non-negative integer. Match the determined n times. For example, "o{2}" cannot match the "o" in "Bob", but it can match both o's in "food".


n is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match the "o" in "Bob", but can match all the o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is then equivalent to "o*".


m harmonyn All are non-negative integer (math.), among othersn<=m。 minimum matchn times and up tom sub-。 for example,“o{1,3}” will match“fooooood” The first three ofo。“o{0,1}” tantamount to“o?”。 Please note that there should be no space between the comma and the two numbers。


Matches all but " " in any single character other than the To match including " ", please use any character like "[. ]" model.


matchx perhapsy。 for example,“z|food” compatible“z” perhaps“food”。“(z|f)ood” conjunction“zood” perhaps“food”。


A collection of characters. Matches any of the contained characters. For example, "[abc]" can match "a" in "plain".


A collection of negative characters. Matches any character that is not contained. For example, "[^abc]" can match the "p" in "plain".


Character range. Matches any character in the specified range. For example, "[a-z]" can match any lowercase character in the range "a" to "z".


Negative character range. Matches any arbitrary character that is not in the specified range. For example, "[^a-z]" can match any character not in the range "a" to "z".


Matches any word character that includes an underscore. Equivalent to "[A-Za-z0-9_]".


Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".


Match any blank character, including spaces, tabs, page breaks, etc. is equivalent to [f v]。


Matches any non-blank character. is equivalent to [^f v]。


Matches a numeric character. is equivalent to [0-9].


Matches a non-numeric character. is equivalent to [^0-9].

Only a few commonly used off-the-shelf ones are documented here regular expression( A verification should suffice for general conditions), note: Don't ever remember.! Just come back for it when you need it.!

User name (6~18 digits, letters, underscore)


Password (consisting of 6 to 18 digits, letters, and underscores)


Password 2

(?=^.{8,}$)(?=.*d)(?=.*W+)(?=.*[A-Z])(?=.*[a-z])(?!.* ).*$ (composed of numbers/capital letters/lowercase letters/punctuation, all four must be present, 8+ digits)

hexadecimal value



/^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/ /^[a-zd]+(.[a-zd]+)*@([da-z](-[da-z])?)+(.{1,2}[a-z]+)+$/ perhapsw+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*

Website (URL)

/^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$/ perhaps [a-zA-z]+://[^s]*

IP address

/((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)/ /^(?:(?: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]?)$/ perhaps ((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)

HTML tag

/^<([a-z]+)([^<]+)*(?:>(.*)</1>|s+/>)$/ perhaps<(.*)(.*)>.*</1>|<(.*) />

Delete code \ comments


Match double-byte characters (including Chinese characters)


Chinese characters (characters)


Range of Chinese characters in Unicode encoding


Chinese and full-angle punctuation (characters)


Date (year-month-day)


Date (month/day/year)


Time (hours:minutes, 24-hour system)


Mainland China Landline Numbers


Mainland China Mobile Number


Postal Code of Mainland China


Mainland China ID Number(15 location perhaps18 location)


Non-negative integers (positive integers or zero)


positive integer (math.)


carry (on one's back) integer (math.)


integer (math.)


small figure


blank line

s* perhaps -er (editplus) perhaps -er ^[sS ]*

QQ number


Words that do not contain abc


Matching first and last blank characters


How to apply regular expression ?

right regular expression after having some knowledge, How to apply it in our program? Application in all environments regular expression (question tag)?

When you search, you will find JS regular expressions, PHP regular expressions, Python regular expressions and so on, which shows that regular expressions are still widely used, and any language environment "knows" it.

However, the application of regular expressions differs in each language environment (after all, each environment calls a different underlying function), for example, we . Classes such as Regex and Match are commonly used in NET to apply expressions, and can be applied directly in JS using methods such as test and match of String objects, and in Java the Pattern class and Matcher class are commonly used.

Listed here are just a few of the ways in which . Use regular expressions in NET and JS, and ask Doula for the rest if you're interested.

(located) at.NET use in regular expression:

Simply draw a form.

quoteSystem.Text.RegularExpressions Namespace, about this namespace can click the link to refer to MSDN, here for the time being, just give an example of using the IsMatch method of the Regex class to verify whether the input string is legal.

// Validating the numbers
        private void btnIsNumber_Click(object sender, EventArgs e)
            string valEx = @"^d+$";      // meaning Validating the numbers of regular expression rules and regulations
            if (!Regex.IsMatch(txtValidate.Text.Trim(),valEx))
                MessageBox.Show(" You did not enter a number!");
                MessageBox.Show(" You entered a number!");
        // Validate mobile phone number
        private void btnIsPhoneNum_Click(object sender, EventArgs e)
            string valEx = @"^d{11}$";    // meaning Validating the numbers of regular expression rules and regulations, Specify the input11 is numerically valid

            if (!Regex.IsMatch(txtValidate.Text.Trim(), valEx))
                MessageBox.Show(" You did not enter a mobile phone number!");
                MessageBox.Show(" You are entering your mobile phone number!");
        // Verify Chinese
        private void btnIsChinese_Click(object sender, EventArgs e)
            string valEx = @"^[u4e00-u9fa5]+$";    // meaning Verify Chinese of regular expression rules and regulations
            if (!Regex.IsMatch(txtValidate.Text.Trim(), valEx))
                MessageBox.Show(" You are not typing in Chinese!");
                MessageBox.Show(" You are typing in Chinese!");
        // verification email format
        private void btnIsEmail_Click(object sender, EventArgs e)
            string valEx = @"^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$"; // Definition Validation email formatted regular expression rules and regulations
            if (!Regex.IsMatch(txtValidate.Text.Trim(), valEx))
                MessageBox.Show(" You entered not email!");
                MessageBox.Show(" You are entering email!");

Click on [ View Results

(located) atJS application in regular expression

JS file code.

<script language="javascript" type="text/javascript">
	function IsNumber()
 	    // Verify the numbers
		var strValidate=document.getElementById("txtValidate").value;
		var strIsNumber=/^d+$/;  
		var b=strIsNumber.test(strValidate);
		alert(" string (computer science) "+strValidate+"  It's digital.");
		alert(" string (computer science) "+strValidate+"  Not a number.");
	function IsPhoneNum()
 	    /verify mobile number
		var strValidate=document.getElementById("txtValidate").value;		 
		var strIsPhoneNum=/^d{11}$/;                                      
		var b=strIsPhoneNum.test(strValidate);
		alert(" string (computer science) "+strValidate+"  It's a cell phone number.");
		alert(" string (computer science) "+strValidate+"  Not a cell phone number.");
	function IsChinese()
 	    // Verify Chinese  
		var strValidate=document.getElementById("txtValidate").value;		 
		var strIsChinese=/^[u4e00-u9fa5]+$/;                                                           
		var b=strIsChinese.test(strValidate);
		alert(" string (computer science) "+strValidate+"  It's Chinese");
		alert(" string (computer science) "+strValidate+"  Not in Chinese");
	function IsEmail()
	    // verification email 
		var strValidate=document.getElementById("txtValidate").value;		 
		var strIsEmail=/^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$/;                                                           
		var b=strIsEmail.test(strValidate);
		alert(" string (computer science) "+strValidate+"  be email");
		alert(" string (computer science) "+strValidate+"  not email");

HTML code.

<html xmlns="">
<script type="text/javascript" src="test.js"></script>
<form name="form1" id="form1" action="" method="post">
     <p align="center">
     <input name="" id="txtValidate" type="text"/>
     <p align="center">  
     <input name="" type="button" value=" Validating the numbers" onclick="javascript:IsNumber()" />
     <input name="" type="button" value=" Validate mobile phone number" onclick="javascript:IsPhoneNum()"/>
     <input name="" type="button" value=" Verify Chinese" onclick="javascript:IsChinese()"/><span style="font-family: Arial, Helvetica, sans-serif;">   </span>
     <input name="" type="button" value=" verification email" onclick="javascript:IsEmail()"/>

Click on [ View Results

2、mysql database with slow query logging turned on
3、1301 Summary StringBufferStringBuilderArrayAdvancedArraysIntegerCharacter
4、Insights from a programmer who has been working for 8 years beware of this kind of thinking that is slowly eating away at your prospects
5、This 2000person team does a business that doesnt make money but is Robin Lis strategic force

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送









    Contact US

    Welcome to Intefrankly.

    Intefrankly is a platform that you can show your opnions related to IT technology, and communicates to the people who interested in IT and Artificial Intelligence area.