PHP Common Regular Expression Boutique

Source: Internet
Author: User
Tags valid alphanumeric characters

^\d+$//matching nonnegative integer (positive integer + 0)
^[0-9]*[1-9][0-9]*$//Matching positive integer
^ ((-\d+) | (0+)) $//matching non positive integer (negative integer + 0)
^-[0-9]*[1-9][0-9]*$//matching negative integers
^-?\d+$//Matching integer
^\d+ (\.\d+)? $//matching 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]*)] $//matching positive floating-point numbers
^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $//matching non-positive floating-point numbers (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]*))] $///Match negative floating-point number
^ (-?\d+) (\.\d+)? $//matching floating-point number
^[a-za-z]+$//Match a string of 26 English letters
^[a-z]+$//Match a string of 26 uppercase letters
^[a-z]+$//Match string consisting of 26 lowercase letters
^[a-za-z0-9]+$//Match a string of numbers and 26 English letters
^\w+$//Match A string of numbers, 26 English letters, or underscores
^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$//matching email address
^[a-za-z]+://Match (\w+ (-\w+) *) (\. ( \w+ (-\w+) *) * (\?\s*)? $//matching URL


An algorithmic program that uses regular expressions to remove repetitive characters from a string:

var s= "Abacabefgeeii"
var s1=s.replace (/(.). *\1/g, "$")
var re=new RegExp ("[" +s1+ "]", "G")
var s2=s.replace (Re, "")
Alert (S1+S2)//Result: ABCEFGI
===============================
If var s = "Abacabefggeeii"
The result is wrong, the result is: ABEICFGG
The ability of regular expressions is limited


1. Confirm valid e-mail format
The following code example uses the static Regex.IsMatch method to verify whether a string is a valid e-mail format. If the string contains a valid e-mail address, the IsValidEmail method returns True, otherwise it returns false without taking any other action. You can use IsValidEmail to filter e-mail addresses that contain invalid characters before an application stores the address in a database or displays it in a asp.net page.

[Visual Basic]
Function IsValidEmail (Strin as String) as Boolean
' Return True if Strin are 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 are 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. Clean the input string
The following code example uses the static Regex.Replace method to extract invalid characters from a string. You can use the CleanInput method defined here to clear out potentially harmful characters that are entered in the Text field of a form that accepts user input. CleanInput returns a string after all non-alphanumeric characters except the @,-(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 date format
The following code example uses the Regex.Replace method to replace the Mm/dd/yy date form with the Dd-mm-yy date form.

[Visual Basic]
Function mdytodmy (input as String) as String
return Regex.Replace (Input, _
"\b (? <month>\d{1,2})/(? <day>\d{1,2})/(? <year>\d{2,4}) \b", _
"${day}-${month}-${year}")
End Function
[C #]
String mdytodmy (String input)
{
return Regex.Replace (Input,
"\\b (? <month>\\d{1,2})/(? <day>\\d{1,2})/(? <year>\\d{2,4}) \\b",
"${day}-${month}-${year}");
}
Regex substitution mode
This example shows how to use a named reverse reference in Regex.Replace replacement mode. Where the substitution expression ${day} Inserts a substring captured by the (? <day>.) group.

There are several static functions that allow you to use a regular expression operation without creating an explicit regular expression object, and the Regex.Replace function is one of them. This will be convenient if you do not want to keep the compiled regular expressions


4. Extract URL Information
The following code example 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 ("^" <proto>\w+)://[^/]+? <port>:\d+)?/", _
regexoptions.compiled)
return R.match (URL). Result ("${proto}${port}")
End Function
[C #]
String Extension (string url)
{
Regex r = new Regex (@ "^ <proto>\w+)://[^/]+? <port>:\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.