1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Regular Expression c# format string

Discussion in 'C#' started by tmbtech, Nov 3, 2009.

  1. #1
    Hello,

    I'm trying to create a regular expression that will reformat a string.

    basically I only want to allow a-z A-Z 0-9 a space and an "x"

    so if there are any other char found it should remove it from the string

    Does anyone know the best way to do this besides just looping through the string and removing the unwanted char
     
    tmbtech, Nov 3, 2009 IP
  2. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #2
    where that string comes from? Is it textbox entry, database record, hardcoded string variable ....?
     
    yugolancer, Nov 5, 2009 IP
  3. tmbtech

    tmbtech Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    just a variable somewhere.

    I figured it out, here is the code just in case anyone wants to use it.

    string pattern = @"[^A-Za-z0-9\s@.]";
    Regex rgx = new Regex(pattern);
    string inputStr = "2o3423u 09809283 asdjflj 39!@#$%^@#$099248";
    string outputStr = rgx.Replace(inputStr, "");

    I changed the code to allow alphanumeric, space, dot, and @
     
    tmbtech, Nov 5, 2009 IP