. Four examples of application of regular expressions under net

Source: Internet
Author: User
Tags date format empty expression net valid alphanumeric characters port number
Example | regular 1. Confirm valid e-mail format





The following code example uses a 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's 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 Replacement 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 can be handy if you don't want to keep 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.