Remove special character from string using RegularExpression in ASP.Net

Last Reply 4 days ago By dharmendr

Posted 4 days ago

hello,

i have this string 

Nikon-D3400-DSLR-Camera-Body-with-Dual-Lens:-AF-P-DX-NIKKOR-18-55-mm-f/3.5---5.6G-VR-+-AF-P-DX-NIKKOR-70-300-mm-f/4.5---6.3G-ED-VR-(16-GB-SD-Card-+-Camera-Bag)--(Black)

i want to remove all specical character and keep only alphabet small and upper case and numbers and their decimal places

except above 2 conditions i want to remove every thing from string.

please advice

Posted 4 days ago

Hi nauna,

Refer below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    string statement = "Nikon-D3400-DSLR-Camera-Body-with-Dual-Lens:-AF-P-DX-NIKKOR-18-55-mm-f/3.5---5.6G-VR-+-AF-P-DX-NIKKOR-70-300-mm-f/4.5---6.3G-ED-VR-(16-GB-SD-Card-+-Camera-Bag)--(Black)";
    statement = System.Text.RegularExpressions.Regex.Replace(statement, @"[^0-9a-zA-Z.]+", "");
    Response.Write(statement);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim statement As String = "Nikon-D3400-DSLR-Camera-Body-with-Dual-Lens:-AF-P-DX-NIKKOR-18-55-mm-f/3.5---5.6G-VR-+-AF-P-DX-NIKKOR-70-300-mm-f/4.5---6.3G-ED-VR-(16-GB-SD-Card-+-Camera-Bag)--(Black)"
    statement = System.Text.RegularExpressions.Regex.Replace(statement, "[^0-9a-zA-Z.]+", "")
    Response.Write(statement)
End Sub

Output

NikonD3400DSLRCameraBodywithDualLensAFPDXNIKKOR1855mmf3.55.6GVRAFPDXNIKKOR70300mmf4.56.3GEDVR16GBSDCardCameraBagBlack