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
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 (?/d{1,2})/(?/d{1,2})/(?/d{2,4})/b", _
"${day}-${month}-${year}")
End Function
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 (?...) 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 ("^ (?/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}");
}
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.