1. Confirm valid email format
The following code example uses the static Regex.IsMatch method to verify whether a string is in a valid email format. The IsValidEmail method returns true if the string contains a valid email address, false otherwise, but takes no other action. You can use IsValidEmail to filter out e-mail addresses that contain invalid characters before your application stores the address in a database or displays it in an 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. 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 clean up potentially harmful characters entered in a text field on a form that accepts user input. CleanInput returns a string after cleaning all non-alphanumeric characters except @, - (hyphen), and . (period).
[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 example 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(?<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 Pattern
This example shows how to use named backreferences in the replacement pattern of Regex.Replace. where the replacement expression ${day} inserts the substring captured by the (?<day>...) group.
The Regex.Replace function is one of several static functions that allow you to operate with regular expressions without creating an explicit regular expression object. This is convenient if you don't want to keep the compiled regular expression.
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}");
}