Php regular expressions-PHP source code

Source: Internet
Author: User
Tags valid email address alphanumeric characters
Ec (2); ^ d + $ match a non-negative integer (positive integer + 0) ^ [0-9] * [1-9] [0-9] * $ match a positive integer ^ (-d +) | (0 + )) $ match a non-positive integer (negative integer + 0) ^-[0-9] * [1-9] [0-9] * $ match a negative integer ^ -? D + $ match integer ^ d + (. d + )? $ Match non-negative floating point number (Positive floating point number + 0) ^ ([0-9] +. script ec (2); script

^ \ D + $ // match a non-negative integer (positive integer + 0)
^ [0-9] * [1-9] [0-9] * $ // match a positive integer
^ (-\ D +) | (0 +) $ // match a non-positive integer (negative integer + 0)
^-[0-9] * [1-9] [0-9] * $ // match a negative integer
^ -? \ D + $ // match the integer
^ \ D + (\. \ d + )? $ // Match non-negative 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] *) $ // matches the Positive floating point number
^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ // Match a 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] *) $ // matches a negative floating point number.
^ (-? \ D +) (\. \ d + )? $ // Match floating point numbers
^ [A-Za-z] + $ // match A string consisting of 26 English letters
^ [A-Z] + $ // match a string consisting of 26 uppercase letters
^ [A-z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^ \ W + $ // match a string consisting of digits, 26 English letters, or underscores
^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ // match the email address
^ [A-zA-z] +: // match (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ // Match the url


Algorithm program that uses regular expressions to remove repeated characters in a string:

Var s = "abacabefgeeii"
Var s1 = s. replace (/(.). * \ 1/g, "$1 ")
Var re = new RegExp ("[" + s1 + "]", "g ")
Var s2 = s. replace (re ,"")
Alert (s1 + s2) // The result is: abcefgi
====================================
If var s = "abacabefggeeii"
The result is incorrect. The result is: abeicfgg.
Limited capabilities of Regular Expressions


1. Confirm the valid email format
The following code example uses the static Regex. IsMatch method to verify whether a string is in a valid email format. If the string contains a valid email address, the IsValidEmail method returns true; otherwise, the return value is false without any other operations. You can use IsValidEmail to filter out email addresses that contain invalid characters before the application stores the addresses in the database or displays them on the ASP. NET page.

[Visual Basic]
Function IsValidEmail (strIn As String) As Boolean
'Return true if strIn is in valid e-mail format.
Return Regex. isMatch (strIn, ("^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ")
End Function
[C #]
Bool IsValidEmail (string strIn)
{
// Return true if strIn is in valid e-mail format.
Return Regex. isMatch (strIn, @ "^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ");
}


2. Clear input strings
The following code example uses the static Regex. Replace method to extract invalid characters from the string. You can use the CleanInput method defined here to clear potentially harmful characters that may be entered in the text field of the form that receives user input. CleanInput returns a string after all non-alphanumeric characters except @,-(hyphen), and. (period) are cleared.

[Visual Basic]
Function CleanInput (strIn As String) As String
'Replace invalid characters with empty strings.
Return Regex. Replace (strIn, "[^ \ w \. @-]", "")
End Function
[C #]
String CleanInput (string strIn)
{
// Replace invalid characters with empty strings.
Return Regex. Replace (strIn, @ "[^ \ w \. @-]", "");
}


3. Change the Date Format
The following code uses the Regex. Replace method to Replace the date format of mm/dd/yy with the date format of dd-mm-yy.

[Visual Basic]
Function MDYToDMY (input As String) As String
Return Regex. Replace (input ,_
"\ B (? \ D {1, 2 })/(? \ D {1, 2 })/(? \ D {2, 4}) \ B ",_
"$ {Day}-$ {month}-$ {year }")
End Function
[C #]
String MDYToDMY (String input)
{
Return Regex. Replace (input,
"\ B (? \ D {1, 2 })/(? \ D {1, 2 })/(? \ D {2, 4}) \ B ",
"$ {Day}-$ {month}-$ {year }");
}
Regex replacement Mode
This example shows how to use a named reverse reference in the Replace mode of Regex. Replace. Here, the replace expression $ {day} is inserted (? .

There are several static functions that allow you to operate using regular expressions without creating explicit Regular Expression objects. The Regex. Replace function is one of them. If you do not want to retain the compiled regular expression, this will bring you convenience.


4. Extract URL Information
The following code uses Match. Result to extract the protocol and port number from the URL. For example, "http://www.contoso.com: 8080/letters/readme.html" will return "http: 8080 ".

[Visual Basic]
Function Extension (url As String) As String
Dim r As New Regex ("^ (? \ W +): // [^/] +? (? : \ D + )? /",_
RegexOptions. Compiled)
Return r. Match (url). Result ("$ {proto }$ {port }")
End Function
[C #]
String Extension (String url)
{
Regex r = new Regex (@ "^ (? \ W +): // [^/] +? (? : \ D + )? /",
RegexOptions. Compiled );
Return r. Match (url). Result ("$ {proto }$ {port }");
}

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.