Only allowing alphanumeric characters and '_' and '-'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DotNetNewbie

    Only allowing alphanumeric characters and '_' and '-'

    Hi,

    I want to parse a string, ONLY allowing alphanumeric characters and
    also the underscore '_' and dash '-' characters.

    Anything else in the string should be removed.

    I think my regex is looking like:

    ^([\w\d_-])*$


    Now if I have this code:

    string username = "mrcsharpis_so_ cool!!!";

    How can I strip all the characters that I dont' want?

  • =?Utf-8?B?S0g=?=

    #2
    RE: Only allowing alphanumeric characters and '_' and '-'

    Regex is a bit overkill for that; you could...

    string str = "AB&*^#Cabc(#&1 23--__";

    StringBuilder sb = new StringBuilder(s tr.Length);

    foreach (char ch in str)
    {
    if (Char.IsLetterO rDigit(ch)
    || ch == '-' || ch == '_')
    {
    sb.Append(ch);
    }
    }

    str = sb.ToString();


    "DotNetNewb ie" wrote:
    Hi,
    >
    I want to parse a string, ONLY allowing alphanumeric characters and
    also the underscore '_' and dash '-' characters.
    >
    Anything else in the string should be removed.
    >
    I think my regex is looking like:
    >
    ^([\w\d_-])*$
    >
    >
    Now if I have this code:
    >
    string username = "mrcsharpis_so_ cool!!!";
    >
    How can I strip all the characters that I dont' want?
    >
    >

    Comment

    • =?UTF-8?B?QXJuZSBWYWpow7hq?=

      #3
      Re: Only allowing alphanumeric characters and '_' and '-'

      KH wrote:
      Regex is a bit overkill for that; you could...
      >
      string str = "AB&*^#Cabc(#&1 23--__";
      >
      StringBuilder sb = new StringBuilder(s tr.Length);
      >
      foreach (char ch in str)
      {
      if (Char.IsLetterO rDigit(ch)
      || ch == '-' || ch == '_')
      {
      sb.Append(ch);
      }
      }
      >
      str = sb.ToString();
      I think that code is an overkill compared to a simple Regex.Replace !

      Arne

      Comment

      • Jesse Houwing

        #4
        RE: Only allowing alphanumeric characters and '_' and '-'

        Hello KH,
        Regex is a bit overkill for that; you could...
        >
        string str = "AB&*^#Cabc(#&1 23--__";
        >
        StringBuilder sb = new StringBuilder(s tr.Length);
        >
        foreach (char ch in str)
        {
        if (Char.IsLetterO rDigit(ch)
        || ch == '-' || ch == '_')
        {
        sb.Append(ch);
        }
        }
        str = sb.ToString();
        Though, should your requirements become more complex, a regex solution like
        the following can be used:

        string cleaned = Regex.Replace(" string to clean", "[^\w\d_-]", "", RegexOptions.No ne);

        Just put all the characters you want to keep into the range above. Everything
        else will be removed.

        Jesse
        >
        "DotNetNewb ie" wrote:
        >
        >Hi,
        >>
        >I want to parse a string, ONLY allowing alphanumeric characters and
        >also the underscore '_' and dash '-' characters.
        >>
        >Anything else in the string should be removed.
        >>
        >I think my regex is looking like:
        >>
        >^([\w\d_-])*$
        >>
        >Now if I have this code:
        >>
        >string username = "mrcsharpis_so_ cool!!!";
        >>
        >How can I strip all the characters that I dont' want?
        >>
        --
        Jesse Houwing
        jesse.houwing at sogeti.nl


        Comment

        • =?Utf-8?B?S0g=?=

          #5
          Re: Only allowing alphanumeric characters and '_' and '-'

          I usually avoid regex's because of performance. In this case I haven't tested
          but would imagine the difference is approximatly "who cares" ... nonetheless
          I just think of regex's as overkill in many situations where people try to
          use them.

          A great way to use them though is to put the pattern in a config file so it
          can be easily changed when requirements change or for different customers w/o
          recompiling the app.


          "Arne Vajhøj" wrote:
          KH wrote:
          Regex is a bit overkill for that; you could...

          string str = "AB&*^#Cabc(#&1 23--__";

          StringBuilder sb = new StringBuilder(s tr.Length);

          foreach (char ch in str)
          {
          if (Char.IsLetterO rDigit(ch)
          || ch == '-' || ch == '_')
          {
          sb.Append(ch);
          }
          }

          str = sb.ToString();
          >
          I think that code is an overkill compared to a simple Regex.Replace !
          >
          Arne
          >

          Comment

          • =?UTF-8?B?QXJuZSBWYWpow7hq?=

            #6
            Re: Only allowing alphanumeric characters and '_' and '-'

            KH wrote:
            I usually avoid regex's because of performance. In this case I haven't tested
            but would imagine the difference is approximatly "who cares" ... nonetheless
            I just think of regex's as overkill in many situations where people try to
            use them.
            Usually fewer lines of code is what is most cost effective overall.

            Regex is simple code (and if the reader knows regex as a general concept
            it is even easy to read) and code that is easy to modify to different
            requirements.

            It does come with a certain overhead. It may not be suited for
            being called billions or trillions of times. But I doubt that was
            the case here (the variable was named 'username').

            Arne

            Comment

            Working...