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. https://blog.csdn.net/huyuyang6688/article/details/11712743

preamble

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:

meta-character

description

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}

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,}

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*".

{n,m}

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.

x|y

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

[xyz]

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

[^xyz]

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

[a-z]

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

[^a-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".

w

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

W

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

s

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

S

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

d

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

D

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)

/^[a-z0-9_-]{6,18}$/

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

/^[a-z0-9_-]{6,18}$/

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-f0-9]{6}|[a-f0-9]{3})$/

email

/^([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

(?<!http:|S)//.*$

Match double-byte characters (including Chinese characters)

[^x00-xff]

Chinese characters (characters)

[u4e00-u9fa5]

Range of Chinese characters in Unicode encoding

/^[u2E80-u9FFF]+$/

Chinese and full-angle punctuation (characters)

[u3000-u301eufe10-ufe19ufe30-ufe44ufe50-ufe6buff01-uffee]

Date (year-month-day)

(d{4}|d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))

Date (month/day/year)

((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(d{4}|d{2})

Time (hours:minutes, 24-hour system)

((1|0?)[0-9]|2[0-3]):([0-5][0-9])

Mainland China Landline Numbers

(d{4}-|d{3}-)?(d{8}|d{7})

Mainland China Mobile Number

1d{10}

Postal Code of Mainland China

[1-9]d{5}

Mainland China ID Number(15 location perhaps18 location)

d{15}(dd[0-9xX])?

Non-negative integers (positive integers or zero)

d+

positive integer (math.)

[0-9]*[1-9][0-9]*

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

-[0-9]*[1-9][0-9]*

integer (math.)

-?d+

small figure

(-?d+)(.d+)?

blank line

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

QQ number

[1-9]d{4,}

Words that do not contain abc

((?!abc)w)+

Matching first and last blank characters

^s*|s*$

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!");
            }
            else
            {
                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!");
            }
            else
            {
                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!");
            }
            else
            {
                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!");
            }
            else
            {
                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);
		if(b)
		{
		alert(" string (computer science) "+strValidate+"  It's digital.");
		}else
		{
		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);
		if(b)
		{
		alert(" string (computer science) "+strValidate+"  It's a cell phone number.");
		}else
		{
		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);
		if(b)
		{
		alert(" string (computer science) "+strValidate+"  It's Chinese");
		}else
		{
		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);
		if(b)
		{
		alert(" string (computer science) "+strValidate+"  be email");
		}else
		{
		alert(" string (computer science) "+strValidate+"  not email");
		}
	}
</script>

HTML code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<script type="text/javascript" src="test.js"></script>
<body>
<form name="form1" id="form1" action="" method="post">
     <p align="center">
     <input name="" id="txtValidate" type="text"/>
     </p>
     <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()"/>
     </p>
</form>
</body>
</html>

Click on [ View Results


Recommended>>
1、ECMAScript2015ES6inNodejsinterpret
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字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多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.